相忘于江湖

不抛弃,不放弃... 请给我勇敢,改变可以改变的;请给我坚强,接受不可以改变的;请给我智慧,分辨这两者。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
共同点:
   IList, List , ArrayList 通俗一点来讲就是广义的数组,C#里面称之为集合。不同于一般的狭义的数组,它们可以存放任意类型的东西,在申明或者赋值的时候指定。比如你写了一个类 Cake,然后想有一个结构来存放很多Cake的实例,那就可以用他们。

   区别:
   IList与List 通俗一点讲,只能存放同一类型的元素。
   比如声明的时候 声明为List<Cake> cakes=new List<Cake>(); 那么就只能用放cake的实例。
   在从cakes这个变量中取出元素的时候,取到的直接就是Cake类型。不需要做强行的转换。

   如果想要让一个‘数组’存放各种类型的实例,比如有Cake,Juice, Bread,那么就用ArrayList
   ArrayList food=new ArrayList();
注意这里没有指定它装的是什么类型的元素,所以可以随便装咯~
当然,好处不能全让你占完了。在‘取’的时候就要麻烦一点了
ArrayList里元素默认是 Object类型的,所以需要强制转换一下。

再来说IList和List的区别:
我的理解是,IList是一个借口,而List是一个确定的类。
接口,当然就需要你去实现它的函数咯,如果你想这些函数有自己的特色,那么就自己写一个类去实现吧!
然后声明的时候:IList<类型> kk=new 你实现的类名<类型>();
当然你可以写成:IList<类型> kk=new List<类型>();
相当于List实现了IList (事实上C# API中是这样定义的)

如果你写成 List<类型> kk=new List<类型>();
那就意味着你的代码,那些操作List的函数不能有丝毫改变,你得按规定办事。写好是什么,你就用什么。

用法:
以上三个集合的用法都很相似,跟Java也很相似
假如有一个 List<Cake> cakes
增、删、改、查的方法:
  cakes.Add(Cake t);//增 cakes.Remove(int index);//删 cakes.Remove(Cake t);//删 cakes[]=//修改的数据 //查或者改 
 更多的操作可以参考C# API

 

 

IList和ArrayList操作性能对比
接Killkill:http://blog.csdn.net/killlkilll/archive/2006/12/23/1457022.aspx
Lazy: http://blog.csdn.net/lazy_/archive/2006/12/24/1458381.aspx

List<T>在创建的时候的时间消耗上明显比ArrayList要大。
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();
         }

     }

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;
         }

     }

执行结果为:
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);
    }
}

 

posted on 2011-05-05 14:00  playman0211  阅读(10363)  评论(0编辑  收藏  举报