您所在的位置: 首页>>开发>>.Net>>c#>>

C#的6种常用集合类大比拼(2)

http://developer.51cto.com  2007-06-28 16:29  清清月儿  Csdn博客  我要评论(0)
  • 摘要:本文先简单说明了集合与数组的区别,然后讲述了6种常用类集合,供大家参考!
  • 标签:C#    集合类

3.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();//弹出最后一项不删除
Console.WriteLine("Peek");
foreach (int i in qu2)
...{
Console.WriteLine(i);
}

while (qu2.Count != 0)
...{
int i = (int)qu2.Dequeue();//清空
qu2.Dequeue();//清空
}
Console.WriteLine("清空");
foreach (int i in qu2)
...{
Console.WriteLine(i);
}
}
}
}

图3

4.Hashtable类

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

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
...{
class Program
...{
public static void Main()
...{

// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add("one", "The");
myHT.Add("two", "quick");
myHT.Add("three", "brown");
myHT.Add("four", "fox");

// Displays the Hashtable.
Console.WriteLine("The Hashtable contains the following:");
PrintKeysAndValues(myHT);
}

public static void PrintKeysAndValues(Hashtable myHT)
...{
foreach (string s in myHT.Keys)
Console.WriteLine(s);

Console.WriteLine(" -KEY- -VALUE-");
foreach (DictionaryEntry de in myHT)
Console.WriteLine(" {0}: {1}", de.Key, de.Value);
Console.WriteLine();
}
}
}

图4


共3页: 上一页 [1] 2 [3] 下一页
【内容导航】
企业级Web2.0 你准备好了么?
浏览器的战国时代
调查:十大发现 解秘技术人
微软出价446亿美元收购雅虎
Sun以10亿美元并购开源数据库厂商MySQL
 
 验证码: (点击刷新验证码)   匿名发表
  • 野蛮生长

  • 作者:冯仑著
  • “地产界的思想家”冯仑纵横生意江湖20年来,第一次系统梳理出书。  三十年来中国民营企业从前公司时代发展到公司时代,21..
Copyright©2005-2008 51CTO.COM 版权所有