C# 集合类[转]
(一)ArrayList类:使用大小可按需动态增加的数组。
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方法出队列。
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方法出栈。
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就相当于一个容器,用于处理和表现类似key/value的键值对,其中key通常可用来快速查找,同时key是区分大小写;vlaue用于存储对应于key的值。每个元素都是一个存储在DictionaryEntry对象中的键值对。键不能为空引用,但值可以。当把某个元素添加到HashTable时,将根据键的哈希代码将元素放入存储桶中。该键的后续查找将使用键的哈希代码只在一个特定存储桶中搜索,这就大大减少为查找一个元素所需的键比较的次数。
HashTable的加载因子确定元素与存储桶的最大比率。加载因子越小,平均查找速度越快,但消耗的内存也增加。默认的加载因子1.0通常提供速度与大小之间的最佳平衡。当创建Hashtable时,也可以指定其他加载因子。
当向HashTable中添加元素时,HashTable的实际加载因子将增加。当实际加载因子达到指定的加载因子时,HashTable中存储桶的数目自动增加到大于当前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"]); //此处将不会有任何输出
}
}
遍历哈希表
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
}
对HashTable进行排序在这里定义的是对key/value键值对中的key按一定规则重新排列,但实际上这个定义是不能实现的。因为我们无法直接在HashTable进行对key进行重新排列,如果需要HashTable提供某种规则的输出,可以采用一种变通的做法:
ArrayList akeys=new ArrayList(ht.Keys);
akeys.Sort(); //按字母顺序进行排序
foreach(string skey in akeys)
{
Console.Write(skey + ":");
Console.WriteLine(ht[skey]);//排序后输出
}
(五)SortedList类:表示键/值对的集合,与哈希表类似,区别在于SortedList中的Key数组是排好序的。
(六)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>集合类来存储数据就方便多了。
鸣谢:
原文作者:moss_tan_jun
原文出处:http://www.cnblogs.com/moss_tan_jun/archive/2010/09/20/1831867.html

浙公网安备 33010602011771号