2016年7月16日 星期六

[C#]ArrayList、List、HashTable、Dictonary有什麼不同?

一、用途

ArrayList、List、HashTable、Dictonary是C#中常用的集合類別,比傳統的陣列固定大小不易增刪,它們都具備add()新增,remove()刪除,clear()清空,contains()是否包含…等方法,當陣列無法滿足你的須求時可以考慮一下這些集合類別。
因為都屬System.Collections命名空間,所以使用前要先using System.Collections匯入命名空間。


二、比較表

 
儲存型態
儲存型別
使用場合
ArrayList
index,value
Object 
使用同一種方式處理集合內所有元素,且元素型別不同
List
index,value
<Type>
使用同一種方式處理集合內所有元素,且元素型別相同
Hashtable
key,value
Object
須用key個別取元素,元素型別不同
Dictionary
key,value
<KeyType,ValueType>
須用key個別取元素,元素型別相同

三、程式範例

1.ArrayList
ArrayList arrayList = new ArrayList() { "a", 1, true };//初始化容器
foreach (Object o in arrayList) { //foreach尋訪容器
 Console.WriteLine(o); 
}
for(int i = 0; i < arrayList.Count; i++){//for尋訪容器
 Console.WriteLine(arrayList[i]);
}
arrayList.Add("new");//新增元素
Console.WriteLine(arrayList.Count);//元素個素
Console.WriteLine(arrayList.Contains(1));//是否包含元素
arrayList.AddRange(new string[] { "x", "y", "z" });//新增陣列至容器
arrayList.Remove(1);//依元素值刪除元素
arrayList.RemoveAt(1);//依元素索引刪除元素
arrayList.Reverse();//容器反轉
arrayList.Sort();//容器排序
arrayList.Clear();//容器清空

  • ArrayList是一種index,value集合,使用上可以像陣列一樣,透過index來指定元素。
  • 容器中的元素是使用Object類別來儲存,雖然可以放不同的型別的元素,但是當使用add或在初始化時傳入引數不論是int,boolean,string,自訂義類別…等都會先裝箱成Object類別,這會大幅降低程式的性能。
2.List
List<string> list = new List<string>() { "s1", "s2", "s3" };//初始化容器
foreach (string s in list) { //foreach尋訪容器
 Console.WriteLine(s); 
}
for(int i = 0; i < list.Count; i++){//for尋訪容器
 Console.WriteLine(list[i]);
}
list.Add("s4");//新增元素
Console.WriteLine(list.Contains("s2"));//是否包含元素
Console.WriteLine(list.Count);//元素個數
list.AddRange(new string[]{ "x", "y", "z" });//新增陣列至容器
list.Remove("s4");//依元素值刪除元素
list.RemoveAt(2);//依元素索引刪除元素
list.Reverse();//容器反轉
list.Sort();//容器排序
list.Clear();//容器清空
  • List和ArrayList一樣是index,value集合,不同在於2006年1月22日公佈的.NET Framework 2.0支援了泛型generics,可以用List<Type>來指定類別,改善ArrayList的效能問題。
3.Hashtable
Hashtable hashtable = new Hashtable();//初始化容器
hashtable.Add("k1", "v1");//新增元素
hashtable.Add("k2", 1);
hashtable.Add("k3", "v3");
foreach (DictionaryEntry d in hashtable){//尋訪容器
 Console.WriteLine(d.Key + ":" + d.Value);
}
Console.WriteLine(hashtable.Count);//元素個數
Console.WriteLine(hashtable.Contains("k1"));//是否包含鍵
Console.WriteLine(hashtable.ContainsValue("v1"));//是否包含值
hashtable.Remove("k2"); //依元素鍵刪除元素
hashtable.Clear();//容器清空
  • Hashtable是key,value集合,透過自訂義的類別key來存取value。陣列是最早儲存集合資料的方式,因為配置記憶體是連續配置,所以在存取時就去記錄第0項的記憶體位置,第1項則是第0項加一個單位型別大小的位置,因此陣列使用index是源自記憶體配置方式。但是如果我希望像google搜尋一樣,輸入關鍵字key,就會跑出相關資料value的話,Hashtable,Dictionary就是不錯的集合類別。ArrayList,List使用Index來編號資料,只是把資料串在一起,Index可能和資料沒有太大關聯,取用時就不像Key,value那麼方便。
  • key,value集合沒有index所以無法用for迴圈來尋訪容器。
  • 另外Hashtable和ArrayList一樣使用Object類別來儲存,也會因裝箱大幅降低程式的性能。
4.Dictionary
Dictionary<string,INT> dictionary = new Dictionary<string,INT>();
dictionary.Add("k1", 1);//新增元素
dictionary.Add("k2", 2);
dictionary.Add("k3", 3);
foreach (KeyValuePair<string,INT> d in dictionary){//尋訪容器
 Console.WriteLine(d.Key + ":" + d.Value);
}
foreach (string k in dictionary.Keys){//尋訪鍵
 Console.WriteLine(k);
}
foreach (int v in dictionary.Values){//尋訪值
 Console.WriteLine(v);
}
Console.WriteLine(dictionary.Count);//元素個數
Console.WriteLine(dictionary.ContainsKey("k1"));//是否包含鍵
Console.WriteLine(dictionary.ContainsValue(1));//是否包含值
dictionary.Remove("k1");//依元素鍵刪除元素
dictionary.Clear();//容器清空
  • 和Hashtable一樣是key,value集合,不同在於2006年1月22日公佈的.NET Framework 2.0支援了泛型generics,可以用Dictionary<Type>來指定類別,改善Hashtable的效能問題。

沒有留言:

張貼留言