1. 程式人生 > >C#非泛型集合類與泛型集合類的區別

C#非泛型集合類與泛型集合類的區別

C# 泛型集合之非泛型集合類與泛型集合類的對應:

ArrayList對應List

HashTable對應Dictionary

Queue對應Queue

Stack對應Stack

SortedList對應SortedList

C# 集合類 Array Arraylist List Hashtable Dictionary Stack Queue
1.陣列是固定大小的,不能伸縮。雖然System.Array.Resize這個泛型方法可以重置陣列大小,
但是該方法是重新建立新設定大小的陣列,用的是舊陣列的元素初始化。隨後以前的陣列就廢棄!而集合卻是可變長的
2.陣列要宣告元素的型別,集合類的元素型別卻是object.
3.陣列可讀可寫不能宣告只讀陣列。集合類可以提供ReadOnly方法以只讀方式使用集合。
4.陣列要有整數下標才能訪問特定的元素,然而很多時候這樣的下標並不是很有用。集合也是資料列表卻不使用下標訪問。
很多時候集合有定製的下標型別,對於佇列和棧根本就不支援下標訪問! 
 

ArrayList 是陣列的複雜版本。ArrayList 類提供在大多數 Collections 類中提供但不在 Array 類中提供的一些功能。例如:

Array 的容量是固定的,而 ArrayList 的容量是根據需要自動擴充套件的。如果更改了 ArrayList.Capacity 屬性的值,則自動進行記憶體重新分配和元素複製。
ArrayList 提供新增、插入或移除某一範圍元素的方法。在 Array 中,您只能一次獲取或設定一個元素的值。
使用 Synchronized 方法可以很容易地建立 ArrayList 的同步版本。而 Array 將一直保持它直到使用者實現同步為止。
ArrayList 提供將只讀和固定大小包裝返回到集合的方法。而 Array 不提供。
另一方面,Array 提供 ArrayList 所不具有的某些靈活性。例如:

可以設定 Array 的下限,但 ArrayList 的下限始終為零。
Array 可以具有多個維度,而 ArrayList 始終只是一維的。
特定型別(不包括 Object)的 Array 的效能比 ArrayList 好,這是因為 ArrayList 的元素屬於 Object 型別,所以在儲存或檢索值型別時通常發生裝箱和取消裝箱。
要求一個數組的大多數情況也可以代之以使用 ArrayList。它更易於使用,並且通常具有與 Object 型別的陣列類似的效能。

Array 位於 System 名稱空間中;ArrayList 位於 System.Collections 名稱空間中。



//陣列
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()複製一個數組

 //ArrayList動態陣列 定義 賦值 輸出  ArrayList可以不用指定維數 可動態賦值  賦不同型別值
            ArrayList arrayList1 = new ArrayList();
            arrayList1.
            arrayList1.Add("a");
            arrayList1.Add(1);
            arrayList1.Add("b");
            Response.Write(arrayList1[1]);

   //Array陣列類 所有陣列的基類 定義 賦值 輸出  Array的容量是固定的 先指定大小 在賦值
            Array arrayList2 = Array.CreateInstance(typeof(string), 6);
            arrayList2.SetValue("a", 0);
            arrayList2.SetValue("b", 1);
            Response.Write(arrayList2.GetValue(1));

   //陣列 定義 賦值 輸出  先指定大小 在賦值
            string[] arrayList;
            arrayList=new string[]{"A","B","C","D"};
            arrayList[0] = "abcde";
            arrayList[2] = "1234";

            arrayList.SetValue("dd", 3);
            Response.Write(arrayList[0]);

   //雜湊表
            Hashtable abc = new Hashtable();
            abc.Add("1", "34");
            if (abc.Contains("1"))
            {
                Response.Write(abc["1"]);
            }

  //宣告一個二維陣列

           int[,] cells=int[3,3];

//初始化一個二維整數陣列

          int[,] cells={{1,0,2},{1,2,0},{1,2,1}};




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