c#中Dictionary、ArrayList、Hashtable和数组 Array 的区别(转)
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());
轉載于:https://www.cnblogs.com/kinghuhua/archive/2010/06/09/1754583.html
總結
以上是生活随笔為你收集整理的c#中Dictionary、ArrayList、Hashtable和数组 Array 的区别(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java对象的理解
- 下一篇: Colaboratory读取谷歌云盘(G