哈希表(HashTable)探究(转)

HashTable 通常称为哈希表,它表示键(key)/值(value)对的集合。

1、HashTable 的构造函数:

           HashTable()              //初始化为0个元素的空实例
           HashTable(Int32)      //初始化为int32 个元素的新实例

           例子:
                      HashTable myHashTable1 = new HashTable();
                      HashTable myHashTable2 = new HashTable(5);

2、HashTable 元素的特点

           HashTable 获取元素的方法是使用“”访问键所对应的值,即HashTable[key]。下面是例子:

           using System;
           using System.Collections.Generic;
           using System.Text;
           using System.Collections;

           namespace Example9_22
           {
               class Program
               {
                   static void Main(string[] args)
                   {
                       Hashtable myHashTable = new Hashtable();
                       //插入相应的键和值组成的元素
                       myHashTable.Add(1,"H");
                       myHashTable.Add(2,"e");
                       myHashTable.Add(3,"l");
                       myHashTable.Add(4,"l");
                       myHashTable.Add(5,"o");
                       myHashTable.Add("int",1);
                       myHashTable.Add("double",2.3);
                       myHashTable.Add("bool",true);
                       //修改相应的键和值组成的元素
                       myHashTable["int"] = 23;
                       myHashTable["double"] = 1.5;
                       myHashTable["bool"] = false;
                       //输出相应的键和值组成的元素
                       Console.WriteLine(myHashTable[1]);
                       Console.WriteLine(myHashTable[2]);
                       Console.WriteLine(myHashTable[3]);
                       Console.WriteLine(myHashTable[4]);
                       Console.WriteLine(myHashTable[5]);
                       Console.WriteLine(myHashTable["int"]);
                       Console.WriteLine(myHashTable["double"]);
                       Console.WriteLine(myHashTable["bool"]);
                       Console.ReadLine();
                   }
               }
           }
           程序运行结果如下:
                                           H
                                           e
                                           l
                                           l
                                           o
                                           23
                                           1.5
                                           False

3、Hashtable 元素的操作

          ● 添加操作的定义如下:
                                                 public virtual void Add(object key,lbject value)

                           注意:“键”不能为空,“值可以为空”。

          ● 删除操作:
                                HashTable.Clear()         //删除所有元素,不带任何参数,清空后HashTable的元素个数为0。
                                        示例:myHashTable.Clear()
                                HashTable.Remove()    //删除指定“键”所在的元素。
                                        其定义如下:
                                                              public virtual void Remove(Object key)
                                        示例:myHashTable.Remove("int");

        如果HashTable 为只读时进行上述操作会出现异常

4、Hashtable 的遍历

         由于Hashtable 的键值对属于DictionaryEntry类型,所以在用foreach 遍历Hashtable 时,类型参数是:DictionaryEntry。看下面示例:
                foreach (DictionatyEntry,myDE in myHashTable)
                {
                     Console.WriteLine("\t{0}\t{1}",myDE.Key,myDE.Value);    //注意读取键值的方法
                }

5、Hashtable 的查询

         Hashtable 查询有以下方法:
                                                       ● Hashtable.Contains            //判断HashTable中是否包含指定“键”。
                                                       ● Hashtable.ContainsKey      //判断HashTable中是否包含指定“键”。同上
                                                       ● Hashtable.ContainsValue   //判断HashTable中是否包含指定值。很好!

         示例:
                    Console.WriteLine("myHashTable 包含键1:{0}",myHashTable.Contains(1));
                    Console.WriteLine("myHashTable 包含键6:{0}",myHashTable.ContainsKey(6));
                    Console.WriteLine("myHashTable 包含值true:{0}",myHashTable.ContainsValue(true));
                    Console.WriteLine("myHashTable 包含值3.3:{0}",myHashTable.ContainsValue(2.2));

6、HashTable 表的优点

         HashTable是System.Collections命名空间提供的一个容器,HashTable中的key/value均为object类型,所以HashTable可以支持任何类型的key/value键/值对。
         HashTable的优点就在于其索引的方式,速度非常快

******************

key区分大小写

提两个问题:
                        △可以输入重复的键吗?
                        △HashTable 可以插入键值吗?

posted on 2008-04-10 20:43  chuncn  阅读(545)  评论(1编辑  收藏  举报

导航