杜思波技术大讨论2009
- System.Collections 命名空间包含接口和类,这些接口和类定义各种对象(如列表、队列、位数组、哈希表和字典)的集合。
- System.Collections.Generic 命名空间包含定义泛型集合的接口和类,泛型集合允许用户创建强类型集合,它能提供比非泛型强类型集合更好的类型安全性和性能。
- System.Collections.Specialized 命名空间包含专用的和强类型的集合,例如,链接的列表词典、位向量以及只包含字符串的集合。
- (一)ArrayList 类:使用大小可按需动态增加的数组。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- ArrayList al = new ArrayList();
- al.Add(100);//单个添加
- foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
- {
- al.Add(number);//集体添加方法一
- }
- int[] number2 = new int[2] { 11, 12 };
- al.AddRange(number2);//集体添加方法二
- al.Remove(3);//移除值为3的
- al.RemoveAt(3);//移除第3个
- ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份
- Console.WriteLine("遍历方法一:");
- foreach (int i in al)//不要强制转换
- {
- Console.WriteLine(i);//遍历方法一
- }
- Console.WriteLine("遍历方法二:");
- for (int i = 0; i < al2.Count; i++)//数组是length
- {
- int number = (int)al2[i];//一定要强制转换
- Console.WriteLine(number);//遍历方法二
- }
- }
- }
- }
- (二)Queue:队列,表示对象的先进先出集合。Enqueue方法入队列,Dequeue方法出队列。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Queue qu = new Queue();
- Queue qu2 = new Queue();
- foreach (int i in new int[4] { 1, 2, 3, 4 })
- {
- qu.Enqueue(i);//入队
- qu2.Enqueue(i);
- }
- foreach (int i in qu)
- {
- Console.WriteLine(i);//遍历
- }
- qu.Dequeue();//出队
- Console.WriteLine("Dequeue");
- foreach (int i in qu)
- {
- Console.WriteLine(i);
- }
- qu2.Peek();//返回位于 Queue 开始处的对象但不将其移除。
- Console.WriteLine("Peek");
- foreach (int i in qu2)
- {
- Console.WriteLine(i);
- }
- }
- }
- }
- (三)Stack:栈,表示对象的简单的后进先出非泛型集合。Push方法入栈,Pop方法出栈。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Stack sk = new Stack();
- Stack sk2 = new Stack();
- foreach (int i in new int[4] { 1, 2, 3, 4 })
- {
- sk.Push(i);//入栈
- sk2.Push(i);
- }
- foreach (int i in sk)
- {
- Console.WriteLine(i);//遍历
- }
- sk.Pop();//出栈
- Console.WriteLine("Pop");
- foreach (int i in sk)
- {
- Console.WriteLine(i);
- }
- sk2.Peek();//弹出最后一项不删除
- Console.WriteLine("Peek");
- foreach (int i in sk2)
- {
- Console.WriteLine(i);
- }
- }
- }
- }
- (四)哈希表
- 一、哈希表(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);
- 下面控制台程序将包含以上所有操作:
- using System;
- using System.Collections; //使用Hashtable时,必须引入这个命名空间
- class hashtable
- {
- public static void Main()
- {
- Hashtable ht=new Hashtable(); //创建一个Hashtable实例
- ht.Add("E","e");//添加key/value键值对
- ht.Add("A","a");
- ht.Add("C","c");
- ht.Add("B","b");
- string s=(string)ht["A"];
- if(ht.Contains("E")) //判断哈希表是否包含特定键,其返回值为true或false
- Console.WriteLine("the E key:exist");
- ht.Remove("C");//移除一个key/value键值对
- Console.WriteLine(ht["A"]);//此处输出a
- ht.Clear();//移除所有元素
- Console.WriteLine(ht["A"]); //此处将不会有任何输出
- }
- }
- 三、遍历哈希表
- 遍历哈希表需要用到DictionaryEntry Object,代码如下:
- for(DictionaryEntry de in ht) //ht为一个Hashtable实例
- {
- Console.WriteLine(de.Key);//de.Key对应于key/value键值对key
- Console.WriteLine(de.Value);//de.Key对应于key/value键值对value
- }
- 四、对哈希表进行排序
- 对哈希表进行排序在这里的定义是对key/value键值对中的key按一定规则重新排列,但是实际上这个定义是不能实现的,因为我们无法直接在Hashtable进行对key进行重新排列,如果需要Hashtable提供某种规则的输出,可以采用一种变通的做法:
- ArrayList akeys=new ArrayList(ht.Keys); //别忘了导入System.Collections
- akeys.Sort(); //按字母顺序进行排序
- foreach(string skey in akeys)
- {
- Console.Write(skey + ":");
- Console.WriteLine(ht[skey]);//排序后输出
- }
- (五)SortedList类:表示键/值对的集合,与哈希表类似,区别在于SortedList中的Key数组排好序的。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- namespace ConsoleApplication1
- {
- class Program
- {
- public static void Main()
- {
- SortedList sl = new SortedList();
- sl["c"] = 41;
- sl["a"] = 42;
- sl["d"] = 11;
- sl["b"] = 13;
- foreach (DictionaryEntry element in sl)
- {
- string s = (string)element.Key;
- int i = (int)element.Value;
- Console.WriteLine("{0},{1}", s, i);
- }
- }
- }
- }
- (六)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> 来存储购物车信息,而不需要任何的类型转化。
- 下面是简单的例子,包括声明,填充键值对,移除键值对,遍历键值对
- Dictionary<string, string> myDic = new Dictionary<string, string>();
- myDic.Add("aaa", "111");
- myDic.Add("bbb", "222");
- myDic.Add("ccc", "333");
- myDic.Add("ddd", "444");
- //如果添加已经存在的键,add方法会抛出异常
- try
- {
- myDic.Add("ddd","ddd");
- }
- catch (ArgumentException ex)
- {
- Console.WriteLine("此键已经存在:" + ex.Message);
- }
- //解决add()异常的方法是用ContainsKey()方法来判断键是否存在
- if (!myDic.ContainsKey("ddd"))
- {
- myDic.Add("ddd", "ddd");
- }
- else
- {
- Console.WriteLine("此键已经存在:");
- }
- //而使用索引器来负值时,如果建已经存在,就会修改已有的键的键值,而不会抛出异常
- myDic ["ddd"]="ddd";
- myDic["eee"] = "555";
- //使用索引器来取值时,如果键不存在就会引发异常
- try
- {
- Console.WriteLine("不存在的键/"fff/"的键值为:" + myDic["fff"]);
- }
- catch (KeyNotFoundException ex)
- {
- Console.WriteLine("没有找到键引发异常:" + ex.Message);
- }
- //解决上面的异常的方法是使用ContarnsKey() 来判断时候存在键,如果经常要取健值得化最好用 TryGetValue方法来获取集合中的对应键值
- string value = "";
- if (myDic.TryGetValue("fff", out value))
- {
- Console.WriteLine("不存在的键/"fff/"的键值为:" + value );
- }
- else
- {
- Console.WriteLine("没有找到对应键的键值");
- }
- //下面用foreach 来遍历键值对
- //泛型结构体 用来存储健值对
- foreach (KeyValuePair<string, string> kvp in myDic)
- {
- Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);
- }
- //获取值得集合
- foreach (string s in myDic.Values)
- {
- Console.WriteLine("value={0}", s);
- }
- //获取值得另一种方式
- Dictionary<string, string>.ValueCollection values = myDic.Values;
- foreach (string s in values)
- {
- Console.WriteLine("value={0}", s);
- }
- 常用的属性和方法如下:
- 常用属性 属性说明
- Comparer 获取用于确定字典中的键是否相等的 IEqualityComparer。
- Count 获取包含在 Dictionary 中的键/值对的数目。
- Item 获取或设置与指定的键相关联的值。
- Keys 获取包含 Dictionary 中的键的集合。
- Values 获取包含 Dictionary 中的值的集合。
- 常用的方法 方法说明
- Add 将指定的键和值添加到字典中。
- Clear 从 Dictionary 中移除所有的键和值。
- ContainsKey 确定 Dictionary 是否包含指定的键。
- ContainsValue 确定 Dictionary 是否包含特定值。
- Equals 已重载。 确定两个 Object 实例是否相等。 (从 Object 继承。)
- GetEnumerator 返回循环访问 Dictionary 的枚举数。
- GetHashCode 用作特定类型的哈希函数。GetHashCode 适合在哈希算法和数据结构(如哈希表)中使用。 (从 Object 继承。)
- GetObjectData 实现 System.Runtime.Serialization.ISerializable 接口,并返回序列化 Dictionary 实例所需的数据。
- GetType 获取当前实例的 Type。 (从 Object 继承。)
- OnDeserialization 实现 System.Runtime.Serialization.ISerializable 接口,并在完成反序列化之后引发反序列化事件。
- ReferenceEquals 确定指定的 Object 实例是否是相同的实例。 (从 Object 继承。)
- Remove 从 Dictionary 中移除所指定的键的值。
- ToString 返回表示当前 Object 的 String。 (从 Object 继承。)
- TryGetValue 获取与指定的键相关联的值。