1.ArrayList
ArrayList類主要用於對一個數組中的元素進行各種處理。在ArrayList中主要使用Add、Remove、RemoveAt、Insert四個方法對棧進行操作。Add方法用於將對象添加到 ArrayList 的結尾處;Remove方法用於從 ArrayList 中移除特定對象的第一個匹配項;RemoveAt方法用於移除 ArrayList 的指定索引處的元素;Insert方法用於將元素插入 ArrayList 的指定索引處
1: using System.Collections;//引入命名空間
2: namespace _4
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 頂部的對象但不將其移除。
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Collections;
6:
7: namespace Stack
8: {
9: class Program
10: {
11: static void Main(string[] args)
12: {
13: Stack<int> stack = new Stack<int>();
14:
15: //入棧操作, 使用push方法向stack的頂部添加資料
16: for (int i = 1; i < 6; i++ )
17: {
18: stack.Push(i);
19: Console.WriteLine("{0}入棧", i);
20: }
21:
22: //返回棧頂元素
23: Console.WriteLine("當前棧頂元素為:{0}", (stack.Peek()).ToString());
24: //出棧
25: Console.WriteLine("移除棧頂元素:{0}", stack.Pop().ToString());
26: //返回棧頂元素
27: Console.WriteLine("當前棧頂元素為:{0}", stack.Peek().ToString());
28:
29: //遍曆棧
30: Console.WriteLine("遍曆棧");
31: foreach (int i in stack)
32: {
33: Console.WriteLine(i);
34: }
35:
36: //清空棧
37: while (stack.Count != 0)
38: {
39: int s = (int)stack.Pop();
40: Console.WriteLine("{0}出棧", s);
41: }
42: }
43: }
44: }
3.Queue
Queue(隊列)類主要實現了一個FIFO(First In First Out,先進先出)的機制。元素在隊列的尾部插入(入隊操作),並從隊列的頭部移出(出隊操作)。在Queue中主要使用Enqueue、Dequeue、Peek三個方法對隊進行操作。Enqueue方法用於將對象添加到 Queue 的結尾處;Dequeue方法移除並返回位於 Queue 開始處的對象;Peek方法用於返回位於 Queue 開始處的對象但不將其移除。
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Collections;
6:
7: namespace Queue
8: {
9: class Program
10: {
11: static void Main(string[] args)
12: {
13: //執行個體化Queue類的對象
14: Queue<int> queue = new Queue<int>();
15:
16: //人隊列,使用enqueue方法向Queue隊列中添加元素
17: for (int i=1; i<6; i++)
18: {
19: queue.Enqueue(i);
20: Console.WriteLine("{0}入隊", i);
21: }
22:
23: //返回隊列開始處的元素
24: Console.WriteLine("當前隊列開始處的元素為:{0}", queue.Peek().ToString());
25: //遍曆隊
26: Console.WriteLine("遍曆隊");
27: foreach (int i in queue)
28: {
29: Console.WriteLine(i);
30: }
31:
32: //清空隊列
33: while (queue.Count != 0)
34: {
35: int q = queue.Dequeue();
36: Console.WriteLine("{0}出隊", q);
37: }
38: }
39: }
40: }
4.Hashtable
Hashtable(雜湊表)是一種鍵/值對集合,這些鍵/值對根據鍵的雜湊碼進行組織。在一個Hashtable中插入一對Key/Value時,它自動將Key值對應到Value,並允許擷取與一個指定的Key相關聯的value。在Hashtable中主要使用Add、Remove兩個方法對雜湊表進行操作。Add方法用於將帶有指定鍵和值的元素添加到 Hashtable 中;Remove方法用於從 Hashtable 中移除帶有指定鍵的元素。
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Collections;
6:
7: namespace HashTable
8: {
9: class Program
10: {
11: static void Main(string[] args)
12: {
13: //執行個體化HashTable類的對象
14: Hashtable student = new Hashtable();
15:
16: //向HashTable中添加元素
17: for (int i = 0; i < 10; i++ )
18: {
19: string key = "s100" + i.ToString();
20: string value = "kof" + i.ToString();
21:
22: //先判斷是否有已經存在的鍵
23: if (!student.ContainsKey(key))
24: {
25: student.Add(key, value);
26: }
27: }
28:
29: //遍曆HashTable
30: foreach (DictionaryEntry element in student)
31: {
32: //擷取或設定索引值對中的鍵
33: string id = element.Key.ToString();
34: //擷取索引值對中的值
35: string name = element.Value.ToString();
36: Console.WriteLine("學生的ID:{0} 學生姓名:{1}", id, name);
37: }
38:
39: //移除HashTable中的元素,根據鍵
40: student.Remove("s1003");
41: Console.WriteLine("已經移除後的雜湊表:");
42:
43: //DictionaryEntry 索引值對對象
44: foreach (DictionaryEntry element in student)
45: {
46: //擷取或設定索引值對中的鍵
47: string id = element.Key.ToString();
48: //擷取索引值對中的值
49: string name = element.Value.ToString();
50: Console.WriteLine("學生的ID:{0} 學生姓名:{1}", id, name);
51: }
52: }
53: }
54: }
5,Dictionary<,>
Dictionary是一個泛型
他本身有集合的功能有時候可以把它看成數組
他的結構是這樣的:Dictionary<[key], [value]>
他的特點是存入對象是需要與[key]值一一對應的存入該泛型
1: Dictionary<int, string> dic = new Dictionary<int, string>();
2:
3: //添加索引值對
4: dic.Add(1, "one");
5: dic.Add(2, "two");
6: dic.Add(3, "three");
7: //提取元素的方法
8: for (int i = 1; i <= 3; i++ )
9: {
10: Console.WriteLine("{0}個元素是{1}", i, dic[i]);
11: }