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

来源:互联网 发布:2017大盘 知乎 编辑:程序博客网 时间:2024/05/22 13:14
C#中Dictionary、ArrayList、Hashtable和Array的区别
C# 泛型集合之非泛型集合类与泛型集合类的对应:
ArrayList对应List
HashTable对应Dictionary
Queue对应Queue
Stack对应Stack
SortedList对应SortedList
 
C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue 


数组(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








222222222222
C#中Dictionary、ArrayList、Hashtable和数组的区别是什么?
1.数组是固定大小的,不能伸缩。虽然System.Array.Resize这个泛型方法可以重置数组大小, 
但是该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃!而集合却是可变长的 
2.数组要声明元素的类型,集合类的元素类型却是object. 
3.数组可读可写不能声明只读数组。集合类可以提供ReadOnly方法以只读方式使用集合。 
4.数组要有整数下标才能访问特定的元素,然而很多时候这样的下标并不是很有用。集合也是数据列表却不使用下标访问。 
很多时候集合有定制的下标类型,对于队列和栈根本就不支持下标访问! 


//数组 
int[] intArray1; 
//初始化已声明的一维数组 
intArray1 = new int[3]; 
intArray1 = new int[3]{1,2,3}; 
intArray1 = new int[]{1,2,3}; 


//ArrayList类对象被设计成为一个动态数组类型,其容量会随着需要而适当的扩充
using System.Collections;
方法 
1:Add()向数组中添加一个元素, 
2:Remove()删除数组中的一个元素 
3:RemoveAt(int i)删除数组中索引值为i的元素 
4:Reverse()反转数组的元素 
5:Sort()以从小到大的顺序排列数组的元素 
6:Clone()复制一个数组 


//List 
可通过索引访问的对象的强类型列表。提供用于对列表进行搜索、排序和操作的方法 
在决定使用 List 还是使用 ArrayList 类(两者具有类似的功能)时,记住 List 类在大多数情况下执行得更好并且是类型安全的。如果对 List 类的类型 T 使用引用类型,则两个类的行为是完全相同的。但是,如果对类型 T 使用值类型,则需要考虑实现和装箱问题。 
如果对类型 T 使用值类型,则编译器将特别针对该值类型生成 List 类的实现。这意味着不必对 List 对象的列表元素进行装箱就可以使用该元素,并且在创建大约 500 个列表元素之后,不对列表元素装箱所节省的内存将大于生成该类实现所使用的内存。 
using System.Collections.Generic;


//Dictionary 
表示键和值的集合。Dictionary遍历输出的顺序,就是加入的顺序,这点与Hashtable不同, 
Dictionary 类与 Hashtable 类的功能相同。对于值类型,特定类型(不包括 Object)的 Dictionary 的性能优于 Hashtable。
using System.Collections.Generic;


//SortedList类 
与哈希表类似,区别在于SortedList中的Key数组排好序的 
using System.Collections.Generic;


//Hashtable类 
哈希表,名-值对。类似于字典(比数组更强大)。哈希表是经过优化的,访问下标的对象先散列过。如果以任意类型键值访问其中元素会快于其他集合。 
GetHashCode()方法返回一个int型数据,使用这个键的值生成该int型数据。哈希表获取这个值最后返回一个索引,表示带有给定散列的数据项在字典中存储的位置。 
using System.Collections;


//Stack类 
栈,后进先出。push方法入栈,pop方法出栈。 
using System.Collections;


//Queue类 
队列,先进先出。enqueue方法入队列,dequeue方法出队列。 
using System.Collections;




------------------------------使用例子------------------------------- 


//Dictionary 
System.Collections.DictionaryEntry dic=new System.Collections.DictionaryEntry("key1","value1"); 


Dictionary<int, string> fruit = new Dictionary<int, string>(); 


//加入重复键会引发异常 
fruit.Add(1, "苹果"); 
fruit.Add(2, "桔子"); 
fruit.Add(3, "香蕉"); 
fruit.Add(4, "菠萝"); 


//因为引入了泛型,所以键取出后不需要进行Object到int的转换,值的集合也一样 
foreach (int i in fruit.Keys) 

     Console.WriteLine("键是:{0} 值是:{1}",i,fruit); 

//删除指定键,值 
fruit.Remove(1); 
//判断是否包含指定键 
if (fruit.ContainsKey(1)) 

     Console.WriteLine("包含此键"); 

//清除集合中所有对象 
fruit.Clear(); 



// Dictionary
System.Collections.Generic.Dictionary<string, bool> proResults = new System.Collections.Generic.Dictionary<string, bool>();
proResults.Clear();
proResults.Add("张三峰", true);
if (probingResults.ContainsKey("张三峰"))
           proResults["张三峰"] = false;
else
           proResults.Add("张三峰", false);




//ArrayList 
System.Collections.ArrayList list=new System.Collections.ArrayList(); 
list.Add(1); 
list.Add(2); 
for(int i=0;i<list.Count;i++) 

     System.Console.WriteLine(list[i]); 





//List 
//声明一个List对象,只加入string参数 
List<string> names = new List<string>(); 
names.Add("乔峰"); 
names.Add("欧阳峰"); 
names.Add("马蜂"); 
//遍历List 
foreach (string name in names) 

     Console.WriteLine(name); 

//向List中插入元素 
names.Insert(2, "张三峰"); 
//移除指定元素 
names.Remove("马蜂"); 




//HashTable 
System.Collections.Hashtable table=new System.Collections.Hashtable(); 
table.Add("table1",1); 
table.Add("table2",2); 
System.Collections.IDictionaryEnumerator d=table.GetEnumerator(); 
while(d.MoveNext()) 

     System.Console.WriteLine(d.Entry.Key); 





//Queue 
System.Collections.Queue queue=new System.Collections.Queue(); 
queue.Enqueue(1); 
queue.Enqueue(2); 


System.Console.WriteLine(queue.Peek()); 
while(queue.Count>0) 

     System.Console.WriteLine(queue.Dequeue()); 





//SortedList 
System.Collections.SortedList list=new System.Collections.SortedList(); 
list.Add("key2",2); 
list.Add("key1",1); 
for(int i=0;i<list.Count;i++) 

     System.Console.WriteLine(list.GetKey(i)); 





//Stack 
System.Collections.Stack stack=new System.Collections.Stack(); 
stack.Push(1); 
stack.Push(2); 


System.Console.WriteLine(stack.Peek()); 
while(stack.Count>0) 

     System.Console.WriteLine(stack.Pop()); 
}
0 0
原创粉丝点击