版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
HashTable 是表示根据键的哈希代码进行组织的键值对(Key/Value)的集合。
首先需要明白的是“键值对”(Key/Value)
例如:学生学号A20190902对应的一个学生“张三”我们可以理解为
Key=A20190902
Value=张三
由于Key相当于索引,所以不能重复,但是值可以重复。
比如李四有很多个银行卡,银行卡的卡号就是Key,这个是不重复的,但是对应的人Value都是李四。
其次,HashTable是一个集合,包含了很多的键值对(Key/Value)。
本节内容将用示例方式讲解HashTable增、删、改、枚举、查找操作。
设计窗口如下:
定义窗体级变量
Dim myHashTab As Hashtable
当窗体载入时候:
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load myHashTab = New Hashtable() End Sub
向myHashTab增加键值对:
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click myHashTab.Add(txtKey.Text, txtValue.Text) End Sub
需要注意的是Add方法: Public Overridable Sub Add ( key As Object, value As Object ) 接受的两个参数都是Object ,也就是任意变量都可以添加为Key或者Value。为了方便掌握,本节内容全部简vb.net教程化为String类型。 当添加了键值对后,采用以下三种方法可以枚举出键值对:
'枚举值
Private Sub showAll_Click(sender As Object, e As EventArgs) Handles showAll.Click
Dim info As String = ""
info &= "键值对个数:" & myHashTab.Count & ControlChars.CrLf
For Each singleHash As DictionaryEntry In myHashTab
info &= singleHash.Key & ":" & singleHash.Value & ":" & singleHash.GetHashCode.ToString("X") & ControlChars.CrLf
singleHash.Key = "111" & singleHash.Key
singleHash.Value = "222" & singleHash.Key
Next
info &= "===============" & ControlChars.CrLf
For Each singleKey As String In myHashTab.Keys
info &= singleKey & ":" & myHashTab(singleKey) & ControlChars.CrLf
Next
info &= "===============" & ControlChars.CrLf
Dim myHashTabEnum As IDictionaryEnumerator = myHashTab.GetEnumerator()
Dim dentry As DictionaryEntry
While myHashTabEnum.MoveNext()
dentry = CType(myHashTabEnum.Current, DictionaryEntry)
info &= dentry.Key & ":" & dentry.Value & ControlChars.CrLf
End While
txtInfo.Text = info
End Sub
按Key查找键值对:
'查找键Key
Private Sub btnFindKey_Click(sender As Object, e As EventArgs) Handles btnFindKey.Click
If myHashTab.ContainsKey(txtFind.Text) Then
txtInfo.Text = "找到Key:" & txtFind.Text & " 对应值:" & myHashTab(txtFind.Text)
Else
txtInfo.Text = "未找到Key:" & txtFind.Text
End If
End Sub
查找Value是否存在:
'查找值Value
Private Sub btnFindValue_Click(sender As Object, e As EventArgs) Handles btnFindValue.Click
If myHashTab.ContainsValue(txtFind.Text) Then
txtInfo.Text = "找到Value:" & txtFind.Text
Else
txtInfo.Text = "未找到Value:" & txtFind.Text
End If
End Sub
删除某个键值对:
'删除键值对
Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click
If myHashTab.ContainsKey(txtFind.Text) Then
myHashTab.Remove(txtFind.Text)
End If
End Sub
修改Value值:
'修改值Value
Private Sub btnEdit_Click(sender As Object, e As EventArgs) Handles btnEdit.Click
If myHashTab.ContainsKey(txtFind.Text) Then
myHashTab(txtFind.Text) = txtEdit.Text
End If
End Sub
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
另外,清除所有键值对:
myHashTab.Clear()
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/uruseibest/article/details/100403354