cakes.Add(Cake t);//增 cakes.Remove(int index);//删 cakes.Remove(Cake t);//删 cakes[]=//修改的数据 //查或者改
更多的操作可以参考C# API
Lazy: http://blog.csdn.net/lazy_/archive/2006/12/24/1458381.aspx
List<T>在创建的时候的时间消耗上明显比ArrayList要大。
List<T>对值类型操作不需要进行装箱;ArrayList需要。
鉴于这两点 ,可以得出,当数据量小的时候呢,ArrayList的操作时间上要比List<T>省,
但是在数据量大的时候呢,List<T>就要比ArrayLIst要省了。
可以来看看下面这个例子:
{
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();
}
}
{
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.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);
}
}