课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - Hashtable哈希表

当前位置:免费教程 » 程序设计 » VB.Net
哈希表Hashtable类表示基于密钥的哈希码组织的键 - 值对的集合。 它使用键来访问集合中的元素。

当您需要使用键访问元素时使用散列表,您可以标识有用的键值。 散列表中的每个项都有一个键/值对。 该键用于访问集合中的项目。


Hashtable类的属性和方法

下表列出了Hashtable类的一些常用属性:
属性描述
CountGets the number of key-and-value pairs contained in the Hashtable.
获取Hashtable中包含的键 - 值对的数量。
IsFixedSizeGets a value indicating whether the Hashtable has a fixed size.
获取指示散列表是否具有固定大小的值。
IsReadOnlyGets a value indicating whether the Hashtable is read-only.
获取一个值,指示Hashtable是否为只读。
ItemGets or sets the value associated with the specified key.
获取或设置与指定键相关联的值。
KeysGets an ICollection containing the keys in the Hashtable.
获取包含Hashtable集合
ValuesGets an ICollection containing the values in the Hashtable.
获取包含Hashtable集合

下表列出了Hashtable类的一些常用方法:
S.N方法名称和用途
1

Public Overridable Sub Add (key As Object, value As Object )

Adds an element with the specified key and value into the Hashtable.

将具有指定键和值的元素添加到Hashtable中。

2

Public Overridable Sub Clear

Removes all elements from the Hashtable.

希表中移除所有元素

3Public Overridable Function ContainsKey (key As Object) As Boolean

Determines whether the Hashtable contains a specific key.

确定希表是否包含特定键。

4Public Overridable Function ContainsValue (value As Object) As Boolean

Determines whether the Hashtable contains a specific value.

确定希表是否包含特定值。

5Public Overridable Sub Remove (key As Object)

Removes the element with the specified key from the Hashtable.

使用指定的键从希表中删除元素。


示例:

下面的例子演示了这个概念:
  1. Module collections
  2. Sub Main()
  3. Dim ht As Hashtable = New Hashtable()
  4. Dim k As String
  5. ht.Add("001", "Zara Ali")
  6. ht.Add("002", "Abida Rehman")
  7. ht.Add("003", "Joe Holzner")
  8. ht.Add("004", "Mausam Benazir Nur")
  9. ht.Add("005", "M. Amlan")
  10. ht.Add("006", "M. Arif")
  11. ht.Add("007", "Ritesh Saikia")
  12. If (ht.ContainsValue("Nuha Ali")) Then
  13. Console.WriteLine("This student name is already in the list")
  14. Else
  15. ht.Add("008", "Nuha Ali")
  16. End If
  17. ' Get a collection of the keys.
  18. Dim key As ICollection = ht.Keys
  19. For Each k In key
  20. Console.WriteLine(" {0} : {1}", k, ht(k))
  21. Next k
  22. Console.ReadKey()
  23. End Sub
  24. End Module

当上述代码被编译和执行时,它产生以下结果:
  1. 006: M. Arif
  2. 007: Ritesh Saikia
  3. 008: Nuha Ali
  4. 003: Joe Holzner
  5. 002: Abida Rehman
  6. 004: Mausam Banazir Nur
  7. 001: Zara Ali
  8. 005: M. Amlan

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号