c#中list arraylist以及ilist区别

1.c#中arraylist所在的命名空间为system.collections,而不是system.collections.genertic。

  共同点:

    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是一个确定的类。
接口,当然就需要你去实现它的函数咯,如果你想这些函数有自己的特色,那么就自己写一个类去实现吧!
然后声明的时候: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要省了。

可以来看看下面这个例子:
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);
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值