C#中Dictionary、ArrayList、Hashtable和Array的区别

 数组(using System)

数组:

数组是固定大小的,不能伸缩,要声明元素的类型。

数组可读可写不能声明只读数组;数组要有整数下标才能访问特定的元素

int[] arry = new int[9];

msdn解释:http://msdn.microsoft.com/zh-cn/library/system.array.aspx

ArrayList(using System.Collections)

1、通过添加和删除元素就可以动态改变数组的长度。但跟一般的数组比起来,速度慢些。

2、ArrayList中的所有元素都是对象的引用(如:ArrayList中的Add()方法定义为public virtual int Add(object value);)

3、ArrayList的索引会自动分配和调整

实例用法:

ArrayList  aList = new ArrayList();

aList.Add("add1"); //将对象添加到尾处:添加后的结果;aList[0] = "add1"

aList.Add("add2");//添加后的结果:aList[0] = "add1",aList[1] ="add2"

aList.Insert(0,"insert1"); //将元素插入指定索引处(插入后的结果:aList[0] ="insert1",aList[1]="add1",aList[2]="add2")

ArrayList aList2 = new ArrayList();

aList2.Add("add3");

aList2.Add("add4");

aList.InsertRange(1,aList2); //插入后结果aList[0]="insert1",aList[1]="add3",aList[2]="add4",aList[3]="add1",aList[4]="add2"

aList.Remove("add3");//移除特定对象的第一个匹配项。移除结果:aList[0]="insert1",aList[1]="add4",aList[2]="add1",aList[3]="add2"

aList.RemoveAt(0); //移除指定索引处的元素。移除结果:aList[0]="add4",aList[1]="add1",aList[2]="add2"

aList.Add("add5");

aList.RemoveRange(1,2);//移除一定范围的元素。1:表示索引;2:表示从索引处开始的数目。移除后结果:aList[0]="add4",aList[1]="add5"

aList.Clear();//移除aList中的所有元素

参考博客:http://www.cnblogs.com/skylaugh/archive/2006/09/15/505346.html

List(using System.Collections.Generic)

List<T> 与 ArrayList的区别
异同点 List<T>

ArrayList

 不同点   对所保存元素做类型约束  可以增加任何类型
 添加/读取无须拆箱、装箱  添加/读取需要拆箱、装箱
 相同点    通过索引访问集合中的元素 
 添加元素方法相同 
 删除元素方法相同 

 List类在大多数情况子下执行得更好并且是类型安全的。

Hashtable(using System.collections)

哈希表(Key-Value):HashTable中的key/value均为object类型,所以HashTable可以支持任何类型的key/value键/值对。其中key必须是唯一的,没有有效的排序。

每个元素是一个存储在DictionaryEntry对象中的键/值dui。key不能为空引用,value可以

类似于字典但比数组更强大。

提供快速查询;元素的存储于顺序无关;因为元素本身没有有效的排序所以不能在指定的位置插入元素。

应用场景:某些数据高频率的查询;大数据量;查询的字段数据类型不是整型、浮点型而是字符串类型

实例用法:

Hashtable ht = new Hashtable

ht.Add("key1","value1");

ht.Add("key1","value2");

ht.Contsins("key1");//判断key1键是否存在

ht.Contsinkey("key1")//同上

foreach(Object key in ht.Keys) //遍历key

{}

foreach(Object value in ht.Values)//遍历value

{}

foreach(DicrionaryEntry de in ht) //同时遍历键/值对

{

        Console.WriteLine(de.Key);

        Console.WriteLine(de.Value);

}

ht.Remove("key1");//移除key1键元素

ht.Clear();//清除所有元素

msdn:http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx

Dictionary(using System.Collections.Generic)

实例应用:

Dictionary<String,String> dy = new Dictionary<String,String>();

dy.Add("key1","value1");//添加

dy["key2"] = "value2";//如果键不存在也可以通过此方法来添加键/值对

dy.ContainsKey("key1"); //判断该键是否存在

dy.Clear();//清除所有

Dictionary<K,V> 和哈希表的对比
异同点 Dictionary<K,V> HashTable
不同点 对所保存元素做类型约束 可以增加任何类型
添加/读取无须拆箱、装箱 添加/读取需要拆箱、装箱
相同点 通过key获取value
添加元素方法相同
删除元素方法相同
遍历方法相同

 对于值类型,特定类型(不包括Object)的Dictionary的性能优于Hashtable。

msdn:http://msdn.microsoft.com/zh-cn/library/xfhwa508(v=vs.80).aspx

Stack(using System.Collections)

栈:后进先出。push()入栈,pop()出栈。

Queue(using System.Collections)

队列:先进先出。enqueue()入队列,dequeue方法出队列

http://blog.csdn.net/hcw_peter/article/details/4737076

原文地址:https://www.cnblogs.com/nanyaxu/p/2796482.html