C#集合

http://www.cnblogs.com/feisky/archive/2009/10/29/1591956.htmlcopy文字说明部分

System.Collections 命名空间包含接口和类,这些接口和类定义各种对象(如列表、队列、位数组、哈希表和字典)的集合。 System.Collections.Generic 命名空间包含定义泛型集合的接口和类,泛型集合允许用户创建强类型集合,它能提供比非泛型强类型集合更好的类型安全性和性能。 System.Collections.Specialized 命名空间包含专用的和强类型的集合,例如,链接的列表词典、位向量以及只包含字符串的集合。

在System.Collections命名空间中提供了许多接口:

  • IEnumerable循环集合项目
  • ICollection可以获取集合中项目个数
  • IList项目列表
  • IDictionary提供了键码索引
 
 

(一)ArrayList 类:使用大小可按需动态增加的数组。

 
  public void ArrayListTest()
        {
            ArrayList aList = new ArrayList();
            aList.Add(1);
 
            int[] intArray = new int[] {1,2,3,56,66 };
            aList.AddRange(intArray);
            Console.WriteLine("----------------show all the data in arraylist-----------------");
            foreach (var item in aList)
            {
                Console.Write(item+"-");
            }
 
        
        }
 
 
 
 

(二)Queue:队列,表示对象的先进先出集合。Enqueue方法入队列,Dequeue方法出队列。

 
     public void QueueTest()
        {
            Queue queue1 = new Queue();
            Queue queue2 = new Queue();
            foreach (var item in new int[]{1,2,3,4 })
            {
                queue1.Enqueue(item);
            }
            Console.WriteLine("print data in queue1");
            foreach (var item in queue1)
            {
                Console.Write(item + "-");
            }
 
           int i= (int)queue1.Dequeue();
           Console.WriteLine();
 
           Console.WriteLine("show the left data after dequeue head data");
           foreach (var item in queue1)
           {
               Console.Write(item+"-");
           }
 
           Console.WriteLine();
        }
 
 
 

(三)Stack:栈,表示对象的简单的后进先出非泛型集合。Push方法入栈,Pop方法出栈。

 
        public void StackTest()
        {
            Stack stack = new Stack();
            stack.Push(1);
 
            foreach (int item in new int[]{78,34,4,54,34})
            {
                stack.Push(item);
            }
 
            Console.WriteLine("---------------show all data in  a stack------------------");
            foreach (int item in stack)
            {
                Console.Write(item+"-");
              
            }
            Console.WriteLine();
            stack.Pop();
 
            Console.WriteLine("show left data when pop the last data");
            foreach (int item in stack)
            {
                Console.Write(item + "-");
            }
 
        }
 
 

(四)哈希表

一、哈希表(Hashtable)简述      在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其中key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中key/value键值对均为object类型,所以Hashtable可以支持任何类型的key/value键值对.
二、哈希表的简单操作 在哈希表中添加一个key/value键值对:HashtableObject.Add(key,value); 在哈希表中去除某个key/value键值对:HashtableObject.Remove(key); 从哈希表中移除所有元素:                HashtableObject.Clear(); 判断哈希表是否包含特定键key:         HashtableObject.Contains(key); 下面控制台程序将包含以上所有操作:
   public void HashTableTest()
        {
            Hashtable hash = new Hashtable();
 
            hash.Add("A""abstractive");
            hash.Add("B","blue");
            hash.Add("C""color");
            hash.Add("D""death");
 
            //judge if exit key of A
            if(hash.ContainsKey("A"))
                Console.WriteLine("the hashtable contains A");
            //print value while key is A
            Console.WriteLine(hash["A"].ToString());
            //remove single value which key is A
            hash.Remove("A");
            try {
                Console.WriteLine(hash["A"].ToString());
            }
            catch
            {
                Console.WriteLine( "A is not exit");
            }
 
            foreach (var item in hash.Values)  //ergodic(遍历) values  in hashtable 
            {
                Console.Write(item+"-");
            }
            foreach (DictionaryEntry item in hash)   //you must use DictionaryEntry
            {
                Console.WriteLine("key:{0},value:{1}", item.Key,item.Value);
            }
            hash.Clear();            //clear all data
        }
 
 
 

(五)SortedList类:表示键/值对的集合,与哈希表类似,区别在于SortedList中的Key数组排好序的。

 
     public void SortedListTest()    //已经排序好
        {
            SortedList sList = new SortedList();
            sList.Add(1, "A");
            sList.Add(456, "C");
            sList.Add(2, "B");
            sList.Add(28,"z");
 
            
            Console.WriteLine( "-----------------SortedList test-------------------");
            foreach (DictionaryEntry item in sList)
            {
                Console.WriteLine("key:{0},value:{1}",item.Key,item.Value);
            }
        }
 
 

(六)Dictionary 泛型集合

泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的装箱和拆箱。     很多非泛型集合类都有对应的泛型集合类,下面是常用的非泛型集合类以及对应的泛型集合类:
非泛型集合类 泛型集合类
ArrayList List<T>
HashTable DIctionary<T>
Queue Queue<T>
Stack Stack<T>
SortedList SortedList<T>
 
我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类。我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和拆箱的负担,如果我们操纵的数据类型相对确定的化  用 Dictionary<TKey,TValue> 集合类来存储数据就方便多了,例如我们需要在电子商务网站中存储用户的购物车信息( 商品名,对应的商品个数)时,完全可以用 Dictionary<string, int> 来存储购物车信息,而不需要任何的类型转化。
    下面是简单的例子,包括声明,填充键值对,移除键值对,遍历键值对
  
 public void DictionaryTest()
        {
          Console.WriteLine("----------------Dictionary Test------------------------");
          Dictionary<intstring> dic = new Dictionary<intstring>();
          dic.Add(1, "a");
          dic.Add(2, "b");
          dic.Add(3, "c");
          dic.Add(4, "d");
          dic.Add(5, "e");
 
          string value= dic.ElementAt(1).ToString();  //print value which index is 1(second one)
          Console.WriteLine(value);         //[2,b]
 
          foreach (var item in dic)
          {
              Console.WriteLine("key:{0},value:{1}",item.Key,item.Value);              
          }
 
          Console.WriteLine("-----------------Different params in Dictionary-----------");
          Dictionary<intstring[]> diction = new Dictionary<intstring[]>();   //params are array
          diction.Add(1,"1,2,34.5,6".Split(','));
 
          Console.Write(diction[1][2]);    //print single value
        }
常用的属性和方法如下:
 
 
常用属性
属性说明
 
获取用于确定字典中的键是否相等的 IEqualityComparer
 
获取包含在 Dictionary 中的键/值对的数目。
 
获取或设置与指定的键相关联的值。
 
获取包含 Dictionary 中的键的集合。
 
获取包含 Dictionary 中的值的集合。
  常用的方法 方法说明
 
将指定的键和值添加到字典中。
 
Dictionary 中移除所有的键和值。
 
确定 Dictionary 是否包含指定的键。
 
确定 Dictionary 是否包含特定值。
 
已重载。 确定两个 Object 实例是否相等。 (从 Object 继承。)
 
返回循环访问 Dictionary 的枚举数。
 
用作特定类型的哈希函数。GetHashCode 适合在哈希算法和数据结构(如哈希表)中使用。 (从 Object 继承。)
 
实现 System.Runtime.Serialization.ISerializable 接口,并返回序列化 Dictionary 实例所需的数据。
 
获取当前实例的 Type。 (从 Object 继承。)
 
实现 System.Runtime.Serialization.ISerializable 接口,并在完成反序列化之后引发反序列化事件。
 
确定指定的 Object 实例是否是相同的实例。 (从 Object 继承。)
 
Dictionary 中移除所指定的键的值。
 
返回表示当前 ObjectString。 (从 Object 继承。)
 
获取与指定的键相关联的值。
原文地址:https://www.cnblogs.com/fjsnail/p/3230324.html