C# 集合类 Array Arraylist List Hashtable Dictionary (三)

四 Dictionary
表示键和值的集合。Dictionary遍历输出的顺序,就是加入的顺序,这点与Hashtable不同
Dictionary<string, string>是一个泛型 ,他本身有集合的功能有时候可以把它看成数组 ,他的结构是这样的:Dictionary<[key], [value]> ,他的特点是存入对象是需要与[key]值一一对应的存入该泛型

 

//实例化对象

Dictionary<int, string> dic = new Dictionary<int, string>();

//对象打点添加

dic.Add(1, "one");

dic.Add(2, "two");

dic.Add(3, "one");

//提取元素的方法

string a = dic[1];

string b = dic[2];

string c = dic[3];

//1、2、3是键,分别对应“one”“two”“one”

//上面代码中分别把值赋给了a,b,c

//注意,键相当于找到对应值的唯一标识,所以不能重复

//但是值可以重复

 

Dictionary<int, string> dic = new Dictionary<int, string>();

dic.Add(1, "HaHa");

dic.Add(5, "HoHo");

dic.Add(3, "HeHe");

dic.Add(2, "HiHi");

dic.Add(4, "HuHu");

 

var result = from pair in dic orderby pair.Key select pair;

 

foreach (KeyValuePair<int, string> pair in result)

{

Console.WriteLine("Key:{0}, Value:{1}", pair.Key, pair.Value);

}

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

 

1、SortedList 的容量是 SortedList 可以保存的元素数。SortedList 的默认初始容量为 0。随着元素添加到 SortedList 中,在需要时可以通过重新分配自动增加容量。可通过调用 TrimToSize方法 或通过显式设置 Capacity 属性减少容量。

2、SortedList 中不允许重复键。

3、SortedList的索引顺序基于排序顺序。当添加元素时,元素将按正确的排序顺序插入 SortedList,同时索引会相应地进行调整。当移除元素时,索引也会相应地进行调整。因此,当在 SortedList 中添加或移除元素时,特定键/值对的索引可能会更改。

4.当不向集合中添加新元素,则调用TrimToSize方法可用于最小化集合的内存开销。

5、通过设置 SortedList 中不存在的键值(例如,myCollection["myNonexistentKey"] = myValue),还可以使用 Item 属性添加新元素。但是,如果指定的键已经存在于 SortedList 中,则设置 Item 属性将改写旧值。相比之下,Add 方法不修改现有元素。

键不能为 空引用(在 Visual Basic 中为 Nothing),但值可以。若要区分由于未找到指定键而返回的 空引用(在 Visual Basic 中为 Nothing) 和由于指定键的值为 空引用(在 Visual Basic 中为 Nothing) 而返回的 空引用(在 Visual Basic 中为 Nothing),请使用 Contains 方法或 ContainsKey 方法确定列表中是否存在该键。



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

1、HashTable定义

System.Collections. Hashtable类表示键/值对的集合,这些键/值对根据键的哈希代码进行组织, 每个元素都是一个存储在 DictionaryEntry 对象中的键/值对。键不能为 null,但值可以。

2.优点

1、通过Key快速查找。

2、Hashtable 是线程安全的。


Hashtable遍历方法

方法一

 foreach (System.Collections.DictionaryEntry objDE in objHasTab)
{
    Console.WriteLine(objDE.Key.ToString());
    Console.WriteLine(objDE.Value.ToString());
}

 方法二

System.Collections.IDictionaryEnumerator enumerator = objHashTablet.GetEnumerator();
while (enumerator.MoveNext())
{
    Console.WriteLine(enumerator.Key);         // Hashtable关健字
    Console.WriteLine

}

Hashtable排序

//把ht的键对象全部复制到ArrayList中

 ArrayList al = new ArrayList(ht.Keys);

 /*ht.Keys返回ht中所有键对象构成的集合,把该集合传递给ArrayList构造方法则得到一个包

*所有键对象的动态数组

*/

  al.Sort();//从小到大排列

  //排序完成输出

   for (int i = 0; i < al.Count;i++ )

   {

          object e=al[i];

          object temp = (object)ht[e];//键作为索引器来获得对应的值对象

           Console.WriteLine(temp.tostring());

}

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

1、Stack定义

System.Collections.Stack类表示对象的简单的后进先出非泛型集合。

2.优点

1、后进先出的集合。

2、Stack 能接受空引用作为有效值并且允许重复的元素

 1、Stack 的容量是 Stack 可以保存的元素数。Stack 的默认初始容量为 10。向 Stack 添加元素时,将通过重新分配来根据需要自动增大容量。

2、容量是 Stack 可存储的元素数。Count 是 Stack 中实际存储的元素数,容量始终大于或等于 Count。如果在添加元素时 Count 超过容量,则通过在复制旧元素和添加新元素之前重新分配内部数组来自动增大容量

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

1、Queue定义

System.Collections.Queue类表示对象的先进先出集合,存储在 Queue(队列) 中的对象在一端插入,从另一端移除。

2.优点

1、能对集合进行顺序处理(先进先出)。

2、能接受null值,并且允许重复的元素。

 

1、Queue 的容量是 Queue 可以保存的元素数。Queue 的默认初始容量为 32。向 Queue 添加元素时,将通过重新分配来根据需要自动增大容量。可通过调用 TrimToSize 来减少容量。等比因子是当需要更大容量时当前容量要乘以的数字。在构造 Queue 时确定增长因子。默认增长因子为 2.0。

2、Queue 能接受空引用作为有效值,并且允许重复的元素。

3、空引用可以作为值添加到 Queue。若要区分空值和 Queue 结尾,请检查 Count 属性或捕捉 Queue 为空时引发的 InvalidOperationException



-------------------------------------------------------------

//Dictionary
System.Collections.DictionaryEntry dic=new System.Collections.DictionaryEntry("key1","value1");

Dictionary<int, string> fruit = new Dictionary<int, string>();

//加入重复键会引发异常
fruit.Add(1, "苹果");
fruit.Add(2, "桔子");
fruit.Add(3, "香蕉");
fruit.Add(4, "菠萝");

//因为引入了泛型,所以键取出后不需要进行Object到int的转换,值的集合也一样
foreach (int i in fruit.Keys)
{
Console.WriteLine("键是:{0} 值是:{1}",i,fruit);
}
//删除指定键,值
fruit.Remove(1);
//判断是否包含指定键
if (fruit.ContainsKey(1))
{
Console.WriteLine("包含此键");
}
//清除集合中所有对象
fruit.Clear();
}


//ArrayList
System.Collections.ArrayList list=new System.Collections.ArrayList();
list.Add(1);
list.Add(2);
for(int i=0;i<list.Count;i++)
{
System.Console.WriteLine(list[i]);
}




//List
//声明一个List对象,只加入string参数
List<string> names = new List<string>();
names.Add("乔峰");
names.Add("欧阳峰");
names.Add("马蜂");
//遍历List
foreach (string name in names)
{
Console.WriteLine(name);
}
//向List中插入元素
names.Insert(2, "张三峰");
//移除指定元素
names.Remove("马蜂");




//HashTable
System.Collections.Hashtable table=new System.Collections.Hashtable();
table.Add("table1",1);
table.Add("table2",2);
System.Collections.IDictionaryEnumerator d=table.GetEnumerator();
while(d.MoveNext())
{
System.Console.WriteLine(d.Entry.Key);
}



//Queue
System.Collections.Queue queue=new System.Collections.Queue();
queue.Enqueue(1);
queue.Enqueue(2);

System.Console.WriteLine(queue.Peek());
while(queue.Count>0)
{
System.Console.WriteLine(queue.Dequeue());
}


//SortedList
System.Collections.SortedList list=new System.Collections.SortedList();
list.Add("key2",2);
list.Add("key1",1);
for(int i=0;i<list.Count;i++)
{
System.Console.WriteLine(list.GetKey(i));
}


//Stack
System.Collections.Stack stack=new System.Collections.Stack();
stack.Push(1);
stack.Push(2);

System.Console.WriteLine(stack.Peek());
while(stack.Count>0)
{
System.Console.WriteLine(stack.Pop());
}
posted @ 2011-05-20 21:22  王海龙(Heaven)  阅读(812)  评论(0编辑  收藏  举报