轻松学习C#的哈希表

时间:2021-05-19

在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#的哈希表相关介绍,希望对大家的学习有所帮助

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章