首页 > 编程 > C# > 正文

轻松学习C#的哈希表

2019-10-29 21:35:17
字体:
来源:转载
供稿:网友

轻松学习C#的哈希表,对C#的哈希表感兴趣的朋友可以参考本篇文章,帮助大家更灵活的运用C#的哈希表。

在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对象的元素的添加

 

 
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections;//需要添加的命名空间  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.  
  8. namespace 哈希表  
  9. {  
  10. class Program  
  11. {  
  12. static void Main(string[] args)  
  13. {  
  14. Hashtable al = new Hashtable();  
  15. Console.WriteLine("添加前al的元素个数为:"+al.Count);  
  16. al.Add("1""a");  
  17. al.Add("2""b");  
  18. al.Add("3""c");  
  19. Console.WriteLine("添加后al的元素个数为:"+al.Count);  
  20. Console.ReadLine();  
  21. }  
  22. }  
  23. }</span>  

输出的结果为:添加前al的元素个数为:0

添加后al的元素个数为:3

二、Hashtable元素的删除

Hashtable对象的元素的删除可通过Remove方法,Clear方法来进行。

(1).Clear方法将清除所有的元素,其格式为:Hashtable对象.Clear()

(2).Remove方法接受一个key参数,作用是移除一个key/value键值对,其格式为:Hashtable对象.Remove()

例二、利用上述方法进行Hashtable元素的删除

 

 
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections;//需要添加的命名空间  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.  
  8. namespace 哈希表  
  9. {  
  10. class Program  
  11. {  
  12. static void Main(string[] args)  
  13. {  
  14. Hashtable al = new Hashtable();  
  15. Console.WriteLine("添加前al的元素个数为:"+al.Count);  
  16. al.Add("1""a");  
  17. al.Add("2""b");  
  18. al.Add("3""c");  
  19. Console.WriteLine("添加后al的元素个数为:"+al.Count);  
  20. al.Remove("3");  
  21. Console.WriteLine("删除3后al的元素个数为:"+al.Count);  
  22. Console.ReadLine();  
  23. }  
  24. }  
  25. }</span>  

输出的结果为:添加前al的元素个数为:0

添加后al的元素个数为:3

删除C后al的元素个数为:2

三、Hashtable元素的遍历

遍历哈希表需要用到DictionaryEntry(字典键/值对)Object。

例三、利用foreach语句对哈希表进行遍历

 

 
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections;//需要添加的命名空间  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.  
  8. namespace 哈希表  
  9. {  
  10. class Program  
  11. {  
  12. static void Main(string[] args)  
  13. {  
  14. Hashtable al = new Hashtable();  
  15. Console.WriteLine("添加前al的元素个数为:"+al.Count);  
  16. al.Add("1""a");  
  17. al.Add("2""b");  
  18. al.Add("3""c");  
  19. Console.WriteLine("添加后al的元素个数为:"+al.Count);  
  20. foreach (DictionaryEntry t in al)  
  21. {  
  22. Console.Write("键位:"+t.Key+" 值为:");  
  23. Console.WriteLine(t.Value);  
  24. }  
  25. Console.ReadLine();  
  26. }  
  27. }  
  28. }</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元素的查找

 

 
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections;//需要添加的命名空间  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.  
  8. namespace 哈希表  
  9. {  
  10. class Program  
  11. {  
  12. static void Main(string[] args)  
  13. {  
  14. Hashtable al = new Hashtable();  
  15. Console.WriteLine("添加前al的元素个数为:"+al.Count);  
  16. al.Add("1""a");  
  17. al.Add("2""b");  
  18. al.Add("3""c");  
  19. Console.WriteLine("添加后al的元素个数为:"+al.Count);  
  20. if (al.Contains("1"))  
  21. {  
  22. Console.WriteLine("1存在al中");  
  23. }  
  24. if (al.ContainsKey("2"))  
  25. {  
  26. Console.WriteLine("2存在al中");  
  27. }  
  28. if (al.ContainsValue("c"))  
  29. {  
  30. Console.WriteLine("c存在al中");  
  31. }  
  32. Console.ReadLine();  
  33. }  
  34. }  
  35. }</span>  

输出的结果为:添加前al的元素个数为:0

添加后al的元素个数为:3

1存在al中

2存在al中

c存在al中

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


注:相关教程知识阅读请移步到c#教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表