1.c#中arraylist所在的命名空间为system.collections,而不是system.collections.genertic。
共同点:
注意这里没有指定它装的是什么类型的元素,所以可以随便装咯~
当然,好处不能全让你占完了。在‘取’的时候就要麻烦一点了
ArrayList里元素默认是 Object类型的,所以需要强制转换一下。
再来说IList和List的区别:
我的理解是,IList是一个借口,而List是一个确定的类。
接口,当然就需要你去实现它的函数咯,如果你想这些函数有自己的特色,那么就自己写一个类去实现吧!
然后声明的时候:I
List<类型> kk=new 你实现的类名<类型>();
当然你可以写成:I
List<类型> kk=new List<类型>();
相当于List实现了IList (事实上C# API中是这样定义的)
如果你写成
List<类型> kk=new List<类型>();
那就意味着你的代码,那些操作List的函数不能有丝毫改变,你得按规定办事。写好是什么,你就用什么。
用法:
以上三个集合的用法都很相似,跟Java也很相似
假如有一个 List<Cake> cakes
List<T>在创建的时候的时间消耗上明显比ArrayList要大。
List<T>对值类型操作不需要进行装箱;ArrayList需要。
鉴于这两点 ,可以得出,当数据量小的时候呢,ArrayList的操作时间上要比List<T>省,
但是在数据量大的时候呢,List<T>就要比ArrayLIst要省了。
可以来看看下面这个例子:
执行结果为:
00:00:00.0005187
00:00:00.0000595
但是当i超过50000条时,大家可以看看执行结果,我在这设置的是1000,0000,其结果为:
Ilist只有 03.8455183
ArrayList 有 20.8369815
List<T>对值类型操作不需要进行装箱;ArrayList需要。
鉴于这两点 ,可以得出,当数据量小的时候呢,ArrayList的操作时间上要比List<T>省,
但是在数据量大的时候呢,List<T>就要比ArrayLIst要省了。
可以来看看下面这个例子:
class
Program
... {
static void Main( string [] args)
... {
Stopwatch sw = new Stopwatch();
sw.Start();
IList < SomeType > list = new List < SomeType > ();
for ( int i = 0 ; i < 1 ; i ++ )
... {
list.Add( new SomeType(i, " test " ));
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
sw.Reset();
sw.Start();
ArrayList al = new ArrayList();
for ( int i = 0 ; i < 1 ; i ++ )
... {
al.Add( new SomeType(i, " test " ));
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
Console.ReadLine();
}
}
... {
static void Main( string [] args)
... {
Stopwatch sw = new Stopwatch();
sw.Start();
IList < SomeType > list = new List < SomeType > ();
for ( int i = 0 ; i < 1 ; i ++ )
... {
list.Add( new SomeType(i, " test " ));
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
sw.Reset();
sw.Start();
ArrayList al = new ArrayList();
for ( int i = 0 ; i < 1 ; i ++ )
... {
al.Add( new SomeType(i, " test " ));
}
sw.Stop();
Console.WriteLine(sw.Elapsed);
Console.ReadLine();
}
}
class
SomeType
... {
public int test_int;
public string test_string;
public SomeType( int test_int, string test_string)
... {
this .test_int = test_int;
this .test_string = test_string;
}
}
... {
public int test_int;
public string test_string;
public SomeType( int test_int, string test_string)
... {
this .test_int = test_int;
this .test_string = test_string;
}
}
执行结果为:
00:00:00.0005187
00:00:00.0000595
但是当i超过50000条时,大家可以看看执行结果,我在这设置的是1000,0000,其结果为:
Ilist只有 03.8455183
ArrayList 有 20.8369815
using System;
using System.Collections;
using System.Collections.Generic;
public class MyClass
{
public static void Main()
{
List<int> iList = new List<int>();
int[] iArray = new int[0];
ArrayList al = new ArrayList();
int count = 10;
//==========================
//增加元素
//==========================
for (int i = 0; i < count; ++i)
{
iList.Add(i);
}
iArray = new int[count];//需要初始化
for (int i = 0; i < count; ++i)
{
iArray[i] = i;
}
for (int i = 0; i < count; ++i)
{
al.Add(i);//这里有box操作
}
//==========================
//输出
//==========================
foreach (int i in iList)
{
Console.WriteLine(i);
}
foreach (int i in iArray)
{
Console.WriteLine(i);
}
foreach (object o in al)
{
Console.WriteLine(o);//这里有unbox操作
}
//============================
//继续增加元素
//============================
iList.Add(count);
iArray = new int[count + 1];//需要重新分配内存
for (int i = 0; i < count; ++i)
{
iArray[i] = i;
}
iArray[count] = count;
al.Add(count);
}
using System.Collections;
using System.Collections.Generic;
public class MyClass
{
public static void Main()
{
List<int> iList = new List<int>();
int[] iArray = new int[0];
ArrayList al = new ArrayList();
int count = 10;
//==========================
//增加元素
//==========================
for (int i = 0; i < count; ++i)
{
iList.Add(i);
}
iArray = new int[count];//需要初始化
for (int i = 0; i < count; ++i)
{
iArray[i] = i;
}
for (int i = 0; i < count; ++i)
{
al.Add(i);//这里有box操作
}
//==========================
//输出
//==========================
foreach (int i in iList)
{
Console.WriteLine(i);
}
foreach (int i in iArray)
{
Console.WriteLine(i);
}
foreach (object o in al)
{
Console.WriteLine(o);//这里有unbox操作
}
//============================
//继续增加元素
//============================
iList.Add(count);
iArray = new int[count + 1];//需要重新分配内存
for (int i = 0; i < count; ++i)
{
iArray[i] = i;
}
iArray[count] = count;
al.Add(count);
}