https://msdn.microsoft.com/zh-cn/library/system.collections(v=vs.110).aspx
https://msdn.microsoft.com/zh-cn/library/system.collections.generic(v=vs.110).aspx
1.ArrayList類
ArrayList類主要用于對一個數(shù)組中的元素進行各種處理。在ArrayList中主要使用Add、Remove、RemoveAt、Insert四個方法對棧進行操作。
Add方法用于將對象添加到?ArrayList?的結(jié)尾處;
Remove方法用于從?ArrayList?中移除特定對象的第一個匹配項;
RemoveAt方法用于移除?ArrayList?的指定索引處的元素;
Insert方法用于將元素插入?ArrayList?的指定索引處。
2.Stack類
Stack(堆棧)類主要實現(xiàn)了一個LIFO(Last In First Out,后進先出)的機制。元素從堆棧的頂部插入(入棧操作),也從堆棧的頂部移除(出棧操作)。在Stack中主要使用Push,Pop,Peek三個方法對棧進行操作。
Push方法用于將對象插入Stack?的頂部;
Pop方法用于移除并返回位于?Stack?頂部的對象;
Peek方法用于返回位于?Stack頂部的對象但不將其移除。
3.Queue類
Queue(隊列)類主要實現(xiàn)了一個FIFO(First In First Out,先進先出)的機制。元素在隊列的尾部插入(入隊操作),并從隊列的頭部移出(出隊操作)。在Queue中主要使用Enqueue、Dequeue、Peek三個方法對隊進行操作。
Enqueue方法用于將對象添加到?Queue?的結(jié)尾處;
Dequeue方法移除并返回位于?Queue?開始處的對象;
Peek方法用于返回位于?Queue?開始處的對象但不將其移除。
4.Hashtable類
Hashtable(哈希表)是一種鍵/值對集合,這些鍵/值對根據(jù)鍵的哈希代碼進行組織。在一個Hashtable中插入一對Key/Value時,它自動將Key值映射到Value,并允許獲取與一個指定的Key相關(guān)聯(lián)的Value。在Hashtable中主要使用Add、Remove兩個方法對哈希表進行操作。
Add方法用于將帶有指定鍵和值的元素添加到 Hashtable 中;
Remove方法用于從?Hashtable?中移除帶有指定鍵的元素。
//實例化Hashtable類的對象
Hashtable?student = new?Hashtable?();
//向Hashtable中添加元素
student.Add("S1001",?"Tom");
student.Add("S1002",?"Jim");
student.Add("S1003",?"Lily");
student.Add("S1004",?"Lucy");
//遍歷Hashtable
foreach?(DictionaryEntry?item in?student)
{
string?id?= item .Key.ToString?();
string?name?= item .Value.ToString?();
Console.WriteLine("學生的ID:{0}???學生姓名:{1}",id,name);
}
//移除Hashtable中的元素
student.Remove("S1002");
說明:Hashtable不能包含重復的key。如果調(diào)用Add方法來添加一個keys數(shù)組中已有的key,就會拋出異常。為了避免這種情況,可以使用ContainsKey ? ? ? 方法來測試哈希表中是否包含一個特定的Key。
5.SortedList類
SortedList類也是鍵/值對的集合,但與哈希表不同的是這些鍵/值對是按鍵排序,并可以按照鍵和索引訪問。在SortedList中主要使用Add、Remove、RemoveAt三個方法對SortedList進行操作。
Add方法用于將帶有指定鍵和值的元素添加到 SortedList 中;
Remove方法用于從?SortedList?中移除帶有指定鍵的元素;
RemoveAt方法用于移除?SortedList?的指定索引處的元素。
//實例化SortedListTest類的對象
SortedList?student?=?new?SortedList();
//向SortedList中添加元素
student.Add("S1001",?"Tom");
student.Add("S1003",?"Jim");
student.Add("S1002",?"Lily");
student.Add("S1004",?"Lucy");
//遍歷SortedList
foreach?(DictionaryEntry?element?in?student)
{
string?id?=?element.Key.ToString();
string?name?=?element.Value.ToString();
Console.WriteLine("學生的ID:{0}???學生姓名:{1}",?id,?name);
}
//移除SortedList中key為“S1003”的元素
student.Remove("S1003");
//移除SortedList中索引為“0”的元素,即第一個元素
student.RemoveAt(0);
說明:同樣SortedList也不能包含重復的key。而且使用foreach語句遍歷SortedList對象時,會返回DictionaryEntry對象。該對象將根據(jù)Key屬性,以排序后的順序返回。
6.Dictionary類
varlist =newDictionary()
{
{"1",1},
{"2",2},
{"3",3}
};
list.Add("a", 1);
list.Add("b", 2);
list.Add("c", 3);
foreach(variteminlist)
{
Console.WriteLine(item.Key + item.Value);
}
//KeyValuePair
foreach(KeyValuePair kvinlist)
{
Console.WriteLine(kv.Key + kv.Value);
}
//通過鍵
foreach(stringkeyinlist.Keys)
{
Console.WriteLine(key + list[key]);
}
//直接取值
foreach(intvalinlist.Values)
{
Console.WriteLine(val);
}
//移除元素
foreach(stringkeyinnewList(list.Keys))
{
if(key =="a")
list.Remove(key);
if(key =="b")
list.Remove(key);
}
stringstr =String.Join(",",list.Values.Select(i => i.ToString()).ToArray());