一. C# 中的集合类 C#中的集合类包括: Stack,Queue,ArraryList,SortedList,HashTable 实例如下: namespace test03 { class Program { static void Main(string[] args) { //1. Stack Console.WriteLine("=================1. Stack================="); Stack stack = new Stack(); foreach (string s in new string[3] { "Easy", " Study ", "C#" }) { stack.Push(s); Console.Write(s); } Console.WriteLine(); Console.WriteLine("---------------------------------------"); stack.Peek(); foreach (string st in stack) { Console.Write(st); } //2. Queue Console.WriteLine(); Console.WriteLine("=================2. Queue================="); Queue q = new Queue(); foreach (string s in new string[3] { "Easy", " Study ", "C#" }) { q.Enqueue(s); Console.Write(s); } Console.WriteLine(); Console.WriteLine("---------------------------------------"); q.Peek(); foreach (string st in q) { Console.Write(st); } //3. ArrayList Console.WriteLine(); Console.WriteLine("=================3. ArrayList================="); ArrayList alist = new ArrayList(); alist.Add(100); foreach (int i in new int[5] { 78, 12, 89, 25, 37 }) { alist.Add(i); } alist.Add(55); alist.Remove(89); alist.Sort(); foreach (int it in alist) { Console.WriteLine(it); } Console.WriteLine("New ArrayList .........."); ArrayList nalist = new ArrayList(alist.GetRange(0, 3)); foreach (int i in nalist) { Console.WriteLine(i); } //4. SortedList Console.WriteLine(); Console.WriteLine("=================4. SortedList================="); SortedList slist = new SortedList(); slist["a"] = 1; slist["b"] = 2; slist["c"] = 3; foreach (DictionaryEntry e in slist) { string key = (string)e.Key; int value = (int)e.Value; Console.WriteLine("{0} : {1}", key, value); } //5. HashTable Console.WriteLine(); Console.WriteLine("=================5. HashTable================="); Hashtable ht = new Hashtable(); ht.Add("a", 36); ht.Add("b", 35); ht.Add("c", 37); ht.Add("d", 34); foreach (DictionaryEntry e in ht) { Console.WriteLine("{0} : {1}",e.Key,e.Value); } Console.WriteLine(""); ArrayList alKeys = new ArrayList(ht.Keys); alKeys.Sort(); foreach (string keys in alKeys) { Console.WriteLine("{0} : {1}", keys, ht[keys]); } if (ht.Contains("f")) { Console.WriteLine("The hastable contains /"c/"."); } Console.ReadKey(); } } }