• 哈希表(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的优点就在于其索引的方式,速度非常快

  • 相关阅读:
    Silverlight 开发环境
    Silverlight通过代码恢复控件属性到默认值
    msn登陆弹出“msnmsgr.exe无法找到入口”的解决办法
    Silverlight UI Designer has thrown an unhandled exception
    数据访问技术路线图
    Could Not Load Type ‘System.ServiceModel.Activation.HttpModule’
    卸载Macfee杀毒软件之后Outlook无法加载项scanotlk.dll,outlook已经将其禁用
    SQL Server 2008 无法生成FRunCM线程
    excel2007数据挖掘客户端看不到
    Windows Server 2008 R2 激活文件备份与还原方法如下
  • 原文地址:https://www.cnblogs.com/jayleke/p/1780506.html
Copyright © 2020-2023  润新知