C#的6种常用集合类

一.先来说说数组不足(也可以说集合与数组的区别

1.数组是固定大小的,不能伸缩。虽然System.Array.Resize这个泛型方法可以重置数组大小,但是该方法是重新创建新设置大小的数组,用的是旧数组的元素初始化。随后以前的数组就废弃!而集合却是可变长的

2.数组要声明元素的类型,集合类的元素类型却是object.

3.数组可读可写不能声明只读数组。集合类可以提供ReadOnly方法以只读方式使用集合。

4.数组要有整数下标才能访问特定的元素,然而很多时候这样的下标并不是很有用。集合也是数据列表却不使用下标访问。很多时候集合有定制的下标类型,对于队列和栈根本就不支持下标访问!

 

二.下面讲述6种常用集合

1.ArrayList类

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Arraylist
  6. {
  7.     class Arraylist
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             ArrayList al = new ArrayList();
  12.             al.Add(100);//单个添加
  13.             foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
  14.             {
  15.                 al.Add(number);//集体添加方法一
  16.             }
  17.             int[] number2 = new int[2] { 11, 12 };
  18.             al.AddRange(number2);//集体添加方法二
  19.             
  20.             al.Remove(3);//移除值为3的
  21.             al.RemoveAt(3);//移除第3个
  22.             ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份
  23.             Console.WriteLine("遍历方法一:");
  24.             foreach (int i in al)//不要强制转换
  25.             {
  26.                 Console.WriteLine(i);
  27.             }
  28.             Console.WriteLine("便利方法二:");
  29.             for (int i = 0; i != al2.Count; i++)//数组是Length
  30.             {
  31.                 int number = (int)al2[i];//一定要强制转换
  32.                 Console.WriteLine(number);
  33.             }
  34.             Console.ReadLine();
  35.         }
  36.     }
  37. }

2.Stack类

栈,后进先出。push方法入栈,pop方法出栈。

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Stacks
  6. {
  7.     class Stacks
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Stack sk = new Stack();
  12.             Stack sk2 = new Stack();
  13.             foreach (int i in new int[4] { 1, 2, 3, 4 })
  14.             {
  15.                 //填充
  16.                 sk.Push(i);
  17.                 sk2.Push(i);
  18.             }
  19.             foreach (int i in sk)//遍历
  20.             {
  21.                 Console.WriteLine(i);
  22.             }
  23.             sk.Pop();//弹出最后一项并且删除
  24.             Console.WriteLine("Pop");
  25.             foreach (int i in sk)
  26.             {
  27.                 Console.WriteLine(i);
  28.             }
  29.             sk2.Peek();//弹出最后一项不删除
  30.             Console.WriteLine("Peek");
  31.             foreach (int i in sk2)
  32.             {
  33.                 Console.WriteLine(i);
  34.             }
  35.             while (sk2.Count != 0)
  36.             {
  37.                 //清空
  38.                 int i = (int)sk2.Pop();
  39.                 sk2.Pop();
  40.             }
  41.             Console.WriteLine("清空");
  42.             foreach (int i in sk2)
  43.             {
  44.                 Console.WriteLine(i);
  45.             }
  46.             Console.ReadLine();
  47.         }
  48.     }
  49. }

3.Queue类

队列,先进先出。enqueue方法入队列,dequeue方法出队列。

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Queues
  6. {
  7.     class Queues
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Queue qu = new Queue();
  12.             Queue qu2 = new Queue();
  13.             
  14.             foreach (int i in new int[4] { 1, 2, 3, 4 })
  15.             {
  16.                 //填充
  17.                 qu.Enqueue(i);
  18.                 qu2.Enqueue(i);
  19.             }
  20.             foreach (int i in qu)//遍历
  21.             {
  22.                 Console.WriteLine(i);
  23.             }
  24.             qu.Dequeue();
  25.             Console.WriteLine("Dequeue");
  26.             foreach (int i in qu)
  27.             {
  28.                 Console.WriteLine(i);
  29.             }
  30.             qu2.Peek();//弹出最后一项不删除
  31.             Console.WriteLine("Peek");
  32.             foreach (int i in qu2)
  33.             {
  34.                 Console.WriteLine(i);
  35.             }
  36.             while (qu2.Count != 0)
  37.             {
  38.                 //清空
  39.                 int i = (int)qu2.Dequeue();
  40.                 qu2.Dequeue();
  41.             }
  42.             Console.WriteLine("清空");
  43.             foreach (int i in qu2)
  44.             {
  45.                 Console.WriteLine(i);
  46.             }
  47.             Console.ReadLine();
  48.         }
  49.     }
  50. }

4.Hashtable类

哈希表,名-值对。类似于字典(比数组更强大)。哈希表是经过优化的,访问下标的对象先散列过。如果以任意类型键值访问其中元素会快于其他集合。GetHashCode()方法返回一个int型数据,使用这个键的值生成该int型数据。哈希表获取这个值最后返回一个索引,表示带有给定散列的数据项在字典中存储的位置。

 

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Hashtables
  6. {
  7.     class Hashtables
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             // Creates and initializes a new Hashtable.
  12.             Hashtable myHT = new Hashtable();
  13.             myHT.Add("one""The");
  14.             myHT.Add("two""quick");
  15.             myHT.Add("three""brown");
  16.             myHT.Add("four""fox");
  17.             // Displays the Hashtable.
  18.             Console.WriteLine("The Hashtable contains the following:");
  19.             PrintKeysAndValues(myHT);
  20.             Console.ReadLine();
  21.         }
  22.         public static void PrintKeysAndValues(Hashtable myHT)
  23.         {
  24.             foreach (string s in myHT.Keys)
  25.                 Console.WriteLine(s);
  26.             Console.WriteLine(" -KEY- -VALUE-");
  27.             foreach (DictionaryEntry de in myHT)
  28.                 Console.WriteLine(" {0}: {1}", de.Key, de.Value);
  29.             Console.WriteLine();
  30.         }
  31.     }
  32. }

5.SortedList类

与哈希表类似,区别在于SortedList中的Key数组排好序的。

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace Sortedlist
  6. {
  7.     class Sortedlist
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             SortedList sl = new SortedList();
  12.             sl["c"] = 41;
  13.             sl["a"] = 42;
  14.             sl["d"] = 11;
  15.             sl["b"] = 13;
  16.             foreach (DictionaryEntry element in sl)
  17.             {
  18.                 string s = (string)element.Key;
  19.                 int i = (int)element.Value;
  20.                 Console.WriteLine("{0},{1}", s, i);
  21.             }
  22.             Console.ReadLine();
  23.         }
  24.     }
  25. }

6.NameValueCollection类

官方给NameValueCollection定义为特殊集合一类,在System.Collections.Specialized下。

System.Collections.Specialized下还有HybridDicionary类,建议少于10个元素用HybridDicionary,当元素增加会自动转为HashTable。

System.Collections.Specialized下还有HybridDicionary类,字符串集合。

System.Collections.Specialized下还有其他类大家可以各取所需!

言归正转主要说NameValueCollection,HashTable 和 NameValueCollection很类似但是他们还是有区别的,HashTable 的KEY是唯一性,而NameValueCollection则不唯一

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Text;
  6. namespace NameValueCollections
  7. {
  8.     class NameValueCollections
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             System.Collections.Hashtable ht = new System.Collections.Hashtable();
  13.             ht.Add("DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
  14.             ht.Add("DdpMNameChi".Trim(), "Name (Chinese)".Trim());
  15.             ht.Add("DdpMNameEng".Trim(), "Name (English)".Trim());
  16.             ht.Add("Comment".Trim(), "Comment".Trim());
  17.             ht.Add("DdpMMarketCode".Trim(), "Market Code".Trim());
  18.             foreach (object key in ht.Keys)
  19.             {
  20.                 Console.WriteLine("{0}/{1}    {2},{3}", key, ht[key], key.GetHashCode(), ht[key].GetHashCode());
  21.             }
  22.             Console.WriteLine(" ");
  23.             NameValueCollection myCol = new NameValueCollection();
  24.             myCol.Add("DdpMDisplaySeq".Trim(), "Display Sequence".Trim());
  25.             myCol.Add("DdpMNameChi".Trim(), "Name (Chinese)".Trim());
  26.             myCol.Add("DdpMNameChi".Trim(), "Name (English)".Trim());
  27.             myCol.Add("Comment".Trim(), "Comment".Trim());
  28.             myCol.Add("DdpMMarketCode".Trim(), "Market Code".Trim());
  29.             foreach (string key in myCol.Keys)
  30.             {
  31.                 Console.WriteLine("{0}/{1} {2},{3}", key, myCol[key], key.GetHashCode(), myCol[key].GetHashCode());
  32.             }
  33.             Console.ReadLine();
  34.         }
  35.     }
  36. }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值