天天看點

c#中Dictionary、ArrayList、Hashtable和數組的差別

http://www.cnblogs.com/peterlu/archive/2011/02/16/1955748.html C# 集合類 Array Arraylist List Hashtable Dictionary Stack Queue

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類對象被設計成為一個動态數組類型,其容量會随着需要而适當的擴充

方法

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 個清單

元素之後,不對清單元素裝箱所節省的記憶體将大于生成該類實作所使用的記憶體。

//Dictionary

表示鍵和值的集合。Dictionary周遊輸出的順序,就是加入的順序,這點與Hashtable不同

//SortedList類

與哈希表類似,差別在于SortedList中的Key數組排好序的

//Hashtable類

哈希表,名-值對。類似于字典(比數組更強大)。哈希表是經過優化的,通路下标的對象先散列過。如果以任意類型鍵值通路其中元素會快于其他集合。

GetHashCode()方法傳回一個int型資料,使用這個鍵的值生成該int型資料。哈希表擷取這個值最後傳回一個索引,表示帶有給定散列的資料項在字典中存儲的位置。

//Stack類

棧,後進先出。push方法入棧,pop方法出棧。

Queue類

隊列,先進先出。enqueue方法入隊列,dequeue方法出隊列。

-------------------------------------------------------------

//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();

}

//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());

}