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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM