1.ArrayList類
ArrayList類主要用於對一個數組中的元素進行各種處理。在ArrayList中主要使用Add、Remove、RemoveAt、Insert四個方法對棧進行操作。Add方法用於將對象添加到ArrayList的結尾處;Remove方法用於從ArrayList中移除特定對象的第一個匹配項;RemoveAt方法用於移除ArrayList的指定索引處的元素;Insert方法用於將元素插入ArrayList的指定索引處。
示例將介紹如何創建一個ArrayList,如何添加項、移除項以用如何遍歷ArrayList。程序代碼如下:
1 using System.Collections;//引入命名空間
2 namespace _1
3 {
4 class ArrayListTest
5 {
6 static void Main(string[] args)
7 {
8 ArrayList arrlist = new ArrayList();//實例化一個ArrayList對象
9 //使用Add方法向ArrayList中添加元素,將元素添加到ArrayList對象的末尾
10 arrlist.Add("蘋果");
11 arrlist.Add("香蕉");
12 arrlist.Add("葡萄");
13 foreach (int n in new int[3] { 0, 1, 2 })
14 {
15 arrlist.Add(n);
16 }
17 //移除值為的第一個元素
18 arrlist.Remove(0);
19 //移除當前索引為的元素,即第個元素
20 arrlist.RemoveAt(3);
21 //在指定索引處添加一個元素
22 arrlist.Insert(1, "apple");
23 //遍歷ArrayList,並輸出所有元素
24 for (int i = 0; i < arrlist.Count; i++)
25 {
26 Console.WriteLine(arrlist[i].ToString());
27 }
28 }
29 }
30 }
2.Stack類
Stack(堆棧)類主要實現了一個LIFO(Last In First Out,後進先出)的機制。元素從棧的頂部插入(入棧操作),也從堆的頂部移除(出棧操作)。在Stack中主要使用Push,Pop,Peek三個方法對棧進行操作。Push方法用於將對象插入Stack的頂部;Pop方法用於移除並返回位於Stack頂部的對象;Peek方法用於返回位於Stack頂部的對象但不將其移除。
示例將介紹如何創建一個Stack,如何添加項、移除項以用如何遍歷Stack。程序代碼如下:
1 using System.Collections;//引入命名空間
2 namespace _2
3 {
4 class StackTest
5 {
6 static void Main(string[] args)
7 {
8 //實例化Stack類的對象
9 Stack stack = new Stack();
10 //入棧,使用Pust方法向Stack對向中添加元素
11 for (int i = 1; i < 6;i++)
12 {
13 stack.Push(i);
14 Console.WriteLine("{0}入棧",i);
15 }
16 //返回棧頂元素
17 Console.WriteLine ("當前棧頂元素為:{0}",stack.Peek().ToString ());
18 //出棧
19 Console.WriteLine("移除棧頂元素:{0}", stack.Pop().ToString());
20 //返回棧頂元素
21 Console.WriteLine("當前棧頂元素為:{0}", stack.Peek().ToString());
22 //遍歷棧
23 Console.WriteLine("遍歷棧");
24 foreach (int i in stack)
25 {
26 Console.WriteLine(i);
27 }
28 //清空棧
29 while(stack .Count!=0)
30 {
31 int s = (int)stack.Pop();
32 Console.WriteLine("{0}出棧",s);
33 }
34 }
35 }
36 }
3.Queue類
Queue(隊列)類主要實現了一個FIFO(First In First Out,先進先出)的機制。元素在隊列的尾部插入(入隊操作),並從隊列的頭部移出(出隊操作)。在Queue中主要使用Enqueue、Dequeue、Peek三個方法對隊進行操作。Enqueue方法用於將對象添加到Queue的結尾處;Dequeue方法移除並返回位於Queue開始處的對象;Peek方法用於返回位於Queue開始處的對象但不將其移除。
示例將介紹如何創建一個Queue,如何添加項、移除項以用如何遍歷Queue。程序代碼如下:
1 using System.Collections;//引入命名空間
2 namespace _3
3 {
4 class QueueTest
5 {
6 static void Main(string[] args)
7 {
8 //實例化Queue類的對象
9 Queue queue = new Queue();
10 //入棧,使用Pust方法向Stack對向中添加元素
11 for (int i = 1; i < 6; i++)
12 {
13 queue .Enqueue(i);
14 Console.WriteLine("{0}入隊", i);
15 }
16 //返回隊開始處的元素
17 Console.WriteLine("當前隊開始處元素為:{0}", queue.Peek().ToString());
18 //遍歷隊
19 Console.WriteLine("遍歷隊");
20 foreach (int i in queue)
21 {
22 Console.WriteLine(i);
23 }
24 //清空棧
25 while (queue.Count != 0)
26 {
27 int q = (int)queue.Dequeue ();
28 Console.WriteLine("{0}出隊", q);
29 }
30 }
31 }
32 }
4.Hashtable類
Hashtable(哈希表)是一種鍵/值對集合,這些鍵/值對根據鍵的哈希代碼進行組織。在一個Hashtable中插入一對Key/Value時,它自動將Key值映射到Value,並允許獲取與一個指定的Key相關聯的value。在Hashtable中主要使用Add、Remove兩個方法對哈希表進行操作。Add方法用於將帶有指定鍵和值的元素添加到Hashtable中;Remove方法用於從Hashtable中移除帶有指定鍵的元素。
示例將介紹如何創建一個Hashtable,如何添加項、移除項以用如何遍歷Hashtable。程序代碼如下:
1 using System.Collections;//引入命名空間
2
3 namespace _4
4 {
5 class HashtableTest
6 {
7 static void Main(string[] args)
8 {
9 //實例化Hashtable類的對象
10 Hashtable student=new Hashtable ();
11 //向Hashtable中添加元素
12 student.Add("S1001","Tom");
13 student.Add("S1002", "Jim");
14 student.Add("S1003", "Lily");
15 student.Add("S1004", "Lucy");
16 //遍歷Hashtable
17 foreach (DictionaryEntry element in student)
18 {
19 string id = element.Key.ToString ();
20 string name = element.Value.ToString ();
21 Console.WriteLine("學生的ID:{0} 學生姓名:{1}",id,name);
22 }
23 //移除Hashtable中的元素
24 student.Remove("S1003");
25 }
26 }
27 }
說明:Hashtable不能包含重復的key。如果調用Add 方法來添加一個keys數組中已有的key,就會拋出異常。為了避免這種情況,可以使用ContainsKey方法來測試哈希表中是否包含一個特定的Key。
5.SortedList類
SortedList類也是鍵/值對的集合,但與哈希表不同的是這些鍵/值對是按鍵排序,並可以按照鍵和索引訪問。在SortedList中主要使用Add、Remove、RemoveAt三個方法對SortedList進行操作。Add方法用於將帶有指定鍵和值的元素添加到SortedList中;Remove方法用於從SortedList中移除帶有指定鍵的元素;RemoveAt方法用於移除SortedList的指定索引處的元素。
示例將介紹如何創建一個SortedList,如何添加項、移除項以用如何遍歷SortedList。程序代碼如下:
1 using System.Collections;//引入命名空間
2
3 namespace _5
4 {
5 class SortedListTest
6 {
7 static void Main(string[] args)
8 {
9 //實例化SortedListTest類的對象
10 SortedList student = new SortedList();
11 //向SortedList中添加元素
12 student.Add("S1001", "Tom");
13 student.Add("S1003", "Jim");
14 student.Add("S1002", "Lily");
15 student.Add("S1004", "Lucy");
16 //遍歷SortedList
17 foreach (DictionaryEntry element in student)
18 {
19 string id = element.Key.ToString();
20 string name = element.Value.ToString();
21 Console.WriteLine("學生的ID:{0} 學生姓名:{1}", id, name);
22 }
23 //移除SortedList中key為“S1003”的元素
24 student.Remove("S1003");
25 //移除SortedList中索引為“”的元素,即第一個元素
26 student.RemoveAt(0);
27 }
28 }
29 }
說明:同樣SortedList也不能包含重復的key。而且使用foreach語句遍歷SortedList對象時,會返回DictionaryEntry對象。該對象將根據Key屬性,以排序後的順序返回。