当前位置: 移动技术网 > IT编程>开发语言>c# > 轻松学习C#的哈希表

轻松学习C#的哈希表

2019年07月18日  | 移动技术网IT编程  | 我要评论

      在c#语言中,还有一种用于快速搜索而组织的键/值组合的数组,这种数组叫做关联数组,也叫做哈希表(hashtable)。
       哈希表也在system.collection命名空间下,用于处理和表现类似key/value的键值对,其中key通常用来快速查找,同时key是区分大小写,且key必须是唯一的。它没有有效的排序,所进行的是内在的排序,value用于存储对应于key的值。哈希表中key/value键值对均为object类型,所以哈希表可以支持任何类型的key/value键值对。哈希表的每个元素是一个存储在dictionaryentry对象中的键值对键值对(所谓的dictionaryentry结构,就是定义可设置或检索的字典键值对,有一个key属性,一个value属性,分别代表键和值)。
       哈希表最大的优点就是把数据的存储和查找消耗的时间大大降低,几乎可以看成常数时间,而代价仅仅是消耗较多的内存。然而在当前可利用内存越来越多的情况下,用空间换时间的做法是值得的。另外,编码比较容易也是它的特点之一。
一、hashtable元素的添加
        hashtable提供了一个添加元素的key/value键值对add方法,该方法有两个参数,一个是键,功能相当于数组中的索引,帮助查找,另一个是值,可以把它看做数组中的元素,其格式为:hashtable对象.add(键,值)
        例一、利用上述的方法进行hashtable对象的元素的添加

<span style="font-size:18px;">using system; 
using system.collections;//需要添加的命名空间 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 哈希表 
{ 
 class program 
 { 
  static void main(string[] args) 
  { 
   hashtable al = new hashtable(); 
   console.writeline("添加前al的元素个数为:"+al.count); 
   al.add("1", "a"); 
   al.add("2", "b"); 
   al.add("3", "c"); 
   console.writeline("添加后al的元素个数为:"+al.count); 
   console.readline(); 
  } 
 } 
}</span> 

输出的结果为:添加前al的元素个数为:0
                       添加后al的元素个数为:3
二、hashtable元素的删除
hashtable对象的元素的删除可通过remove方法,clear方法来进行。
        (1).clear方法将清除所有的元素,其格式为:hashtable对象.clear()
        (2).remove方法接受一个key参数,作用是移除一个key/value键值对,其格式为:hashtable对象.remove()
例二、利用上述方法进行hashtable元素的删除

<span style="font-size:18px;">using system; 
using system.collections;//需要添加的命名空间 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 哈希表 
{ 
 class program 
 { 
  static void main(string[] args) 
  { 
   hashtable al = new hashtable(); 
   console.writeline("添加前al的元素个数为:"+al.count); 
   al.add("1", "a"); 
   al.add("2", "b"); 
   al.add("3", "c"); 
   console.writeline("添加后al的元素个数为:"+al.count); 
   al.remove("3"); 
   console.writeline("删除3后al的元素个数为:"+al.count); 
   console.readline(); 
  } 
 } 
}</span> 

输出的结果为:添加前al的元素个数为:0
                       添加后al的元素个数为:3
                       删除c后al的元素个数为:2
三、hashtable元素的遍历
         遍历哈希表需要用到dictionaryentry(字典键/值对)object。
例三、利用foreach语句对哈希表进行遍历

<span style="font-size:18px;">using system; 
using system.collections;//需要添加的命名空间 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 哈希表 
{ 
 class program 
 { 
  static void main(string[] args) 
  { 
   hashtable al = new hashtable(); 
   console.writeline("添加前al的元素个数为:"+al.count); 
   al.add("1", "a"); 
   al.add("2", "b"); 
   al.add("3", "c"); 
   console.writeline("添加后al的元素个数为:"+al.count); 
   foreach (dictionaryentry t in al) 
   { 
    console.write("键位:"+t.key+" 值为:"); 
    console.writeline(t.value); 
   } 
   console.readline(); 
  } 
 } 
}</span> 

输出的结果为:添加前al的元素个数为:0
                       添加后al的元素个数为:3
                       键位:1 值为:a
                       键位:2 值为:b
                       键位:3 值为:c
四、hashtable元素的查找
       hashtable集合提供三个查找方法查找hashtable中的元素,这三个方法为contains方法,containske和方法和containsvalue方法。
       contains方法,containskey方法是根据hashtable的key值去查找,如果找到,返回匹配的最后一项的自0开始的索引,否则返回-1,其格式为:
       hashtable对象.contains(key值)或 hashtable对象.containskey(key值)
       containvalue方法是根据hashtable的value值去查找,如果找到,返回匹配的最后一项自0开始的索引,否则,返回-1,其格式为:hashtable对象.containsvalue(value值) 
例四、利用上述的方法进行hashtable元素的查找

<span style="font-size:18px;">using system; 
using system.collections;//需要添加的命名空间 
using system.collections.generic; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
 
namespace 哈希表 
{ 
 class program 
 { 
  static void main(string[] args) 
  { 
   hashtable al = new hashtable(); 
   console.writeline("添加前al的元素个数为:"+al.count); 
   al.add("1", "a"); 
   al.add("2", "b"); 
   al.add("3", "c"); 
   console.writeline("添加后al的元素个数为:"+al.count); 
   if (al.contains("1")) 
   { 
    console.writeline("1存在al中"); 
   } 
   if (al.containskey("2")) 
   { 
    console.writeline("2存在al中"); 
   } 
   if (al.containsvalue("c")) 
   { 
    console.writeline("c存在al中"); 
   } 
   console.readline(); 
  } 
 } 
}</span> 

输出的结果为:添加前al的元素个数为:0
                       添加后al的元素个数为:3
                       1存在al中
                       2存在al中
                       c存在al中

以上就是关于c#的哈希表相关介绍,希望对大家的学习有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网