.Net的集合類
在.NetFramework中集合類有很多種,比如:Array(數組),ArrayList(數組列表),List(列表),HashTable(哈希表),Dictionary(字典),Stack(堆棧) ,Queue(隊列)
ArrayList是數組的復雜版本,ArrayList 類提供在大多數Collection類中不提供但不在Array類提供的一些功能,例如:
1、Array的容量是固定的,而ArrayList的容量是根據需要自動擴展的。如果更改了ArrayList.Capacity屬性的值。則自動進行內存重新分配和元素復制。
2、特定類型(不包括Object)的Array的性能比ArrayList好,這是因為ArrayList的元素屬於Object類型,所以在存儲或檢索值類型時通常會發生裝箱和拆箱
Array位於 System命名空間,而ArrayList位於 Systm.Collections 命名空間
C#集合的命名空間
C#集合的三大命名空間:
ArrayList
An ArrayList is a collection from a standard System.Collections namespace. It is a dynamic array. It provides random access to its elements. An ArrayList automatically expands as data is added. Unlike arrays, an ArrayList can hold data of multiple data types. Elements in the ArrayList are accessed via an integer index. Indexes are zero based. Indexing of elements and insertion and deletion at the end of the ArrayList takes constant time. Inserting or deleting an element in the middle of the dynamic array is more costly. It takes linear time.
一個ArrayList是標准System.Collctions命名空間。它是一個動態數組。它提供了隨機訪問元素。一個ArrayList自動擴展數據添加。與數組,數組列表可以容納多個數據類型的數據。數組列表中的元素通過一個整數索引訪問。索引是零基礎。元素的索引,插入和刪除的ArrayList需要持續時間。插入或刪除一個元素的動態數組更昂貴。線性時間。
List
A List
is a strongly typed list of objects that can be accessed by index. It can be found under System.Collections.Generic namespace.
List是一種強類型列表可以通過索引訪問的對象的列表。它可以發現在System.Collections.Generic 命名空間。
LinkedList
鏈表是一個通用的雙向鏈表在c#。LinkedList只允許順序存取。LinkedList允許常量時間插入或者刪除操作,但只有順序存取的元素。由於引用鏈表需要額外的存儲,它們是不切實際等數據項列表的小角色。不同於動態數組,任意數量的物品可以被添加到鏈表(當然內存限制)而不需要realocate,這是一項昂貴的操作。
LinkedList示例
using System;
using System.Collections.Generic;
public class LinkedListExample
{
static void Main()
{
LinkedList<int> nums = new LinkedList<int>();
nums.AddLast(23);
nums.AddLast(34);
nums.AddLast(33);
nums.AddLast(11);
nums.AddLast(6);
nums.AddFirst(9);
nums.AddFirst(7);
LinkedListNode<int> node = nums.Find(6);
nums.AddBefore(node, 5);
foreach(int num in nums)
{
Console.WriteLine(num);
}
}
}
運行結果
Dictionary
字典的檢索和添加值是非常快的,但字典需要更多的內存,因為每個值也有鑰匙。也被稱為一個關聯數組,字典是一組獨特的鍵和值的集合,其中每字典需要更多的內存,因為每一個value都有對應的key
using System;
using System.Collections.Generic;
public class DictionaryExample
{
static void Main()
{
Dictionary<string, string> domains = new Dictionary<string, string>();
domains.Add("de", "Germany");
domains.Add("sk", "Slovakia");
domains.Add("us", "United States");
domains.Add("ru", "Russia");
domains.Add("hu", "Hungary");
domains.Add("pl", "Poland");
Console.WriteLine(domains["sk"]);
Console.WriteLine(domains["de"]);
Console.WriteLine("Dictionary has {0} items",
domains.Count);
Console.WriteLine("Keys of the dictionary:");
List<string> keys = new List<string>(domains.Keys);
foreach(string key in keys)
{
Console.WriteLine("{0}", key);
}
Console.WriteLine("Values of the dictionary:");
List<string> vals = new List<string>(domains.Values);
foreach(string val in vals)
{
Console.WriteLine("{0}", val);
}
Console.WriteLine("Keys and values of the dictionary:");
foreach(KeyValuePair<string, string> kvp in domains)
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
}
}
Queues
A queue is a First-In-First-Out (FIFO) data structure. The first element added to the queue will be the first one to be removed. Queues may be used to process messages as they appear or serve customers as they come. The first customer which comes should be served first.
隊列是先進先出(FIFO)數據結構。第一個元素添加到隊列時會將原來的第一個刪除。隊列可以用來處理消息時出現或按照客戶來的順序。第一個來的客戶應該放在第一位。
示例代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyCollection
{
class QueueExample
{
static void Main()
{
Queue<string> msgs = new Queue<string>();
//入棧[將對象添加到 Queue 的結尾處]
msgs.Enqueue("Message 1");
msgs.Enqueue("Message 2");
msgs.Enqueue("Message 3");
msgs.Enqueue("Message 4");
msgs.Enqueue("Message 5");
//出棧[移除並返回位於 Queue 開始處的對象]
Console.WriteLine(msgs.Dequeue());
//返回位於 Queue 開始處的對象但不將其移除
Console.WriteLine(msgs.Peek());
Console.WriteLine(msgs.Peek());
Console.WriteLine();
foreach (string msg in msgs)
{
Console.WriteLine(msg);
}
}
}
}
運行結果
更多知識
關於隊列的更多知識請參考:http://www.cnblogs.com/tianzhiliang/archive/2010/09/21/1832664.html
Stacks
表示相同任意類型的實例的可變大小的后進先出 (LIFO) 集合。
示例代碼
using System;
using System.Collections.Generic;
namespace MyCollection
{
public class StackExample
{
static void Main()
{
Stack<int> stc = new Stack<int>();
//調用Push方法壓入堆棧
stc.Push(1);
stc.Push(4);
stc.Push(3);
stc.Push(6);
stc.Push(4);
//元素出堆
Console.WriteLine(stc.Pop());
//獲取頂部元素對象,並不移除頂部元
Console.WriteLine(stc.Peek());
Console.WriteLine(stc.Peek());
Console.WriteLine();
foreach (int item in stc)
{
Console.WriteLine(item);
}
}
}
}
運行結果
更多資料
更多請查看MSDN:http://msdn.microsoft.com/zh-cn/library/3278tedw(v=vs.110).aspx
Queue,Stack比較
關於Queue、Stack和其它集合的區別,我覺得主要區別:
Queue和Stack的用在特定的情況下,它們的一個特性是,出棧會把元素刪除,並且是按照入棧的順序出棧的
文獻資料
HashTable和Dictionry的比較->參考:http://blog.csdn.net/snlei/article/details/3939206
MSDN:http://msdn.microsoft.com/zh-cn/library/ybcx56wz.aspx
C#知識網站:http://www.w3cschool.cc/csharp/csharp-tutorial.html
推薦一篇好文:http://www.cnblogs.com/jesse2013/p/CollectionsInCSharp.html