VB.net Dictionary型

ハッシュテーブル(連想配列)を使うには?より。
覚書
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
Dim dict As New Dictionary(Of String, String)
 
' 要素の追加その1
dict("japan") = "日本"
' 要素の追加その2
dict.Add("india", "インド")
' 値の取得その1
Dim val As String = dict("japan")
Console.WriteLine(val) ' 出力:日本
' 値の取得その2
Dim value As String = ""
If dict.TryGetValue("america", value)
  Console.WriteLine(value) ' 出力:アメリカ
End If
' キーの列挙
For Each key As String In dict.Keys
  Console.WriteLine(key)
Next
' 値の列挙
For Each v As String in dict.Values
  Console.WriteLine(v)
Next
' キーの存在チェック
If Not dict.ContainsKey("france")
  ' 存在しない場合
  dict("france") = "フランス"
End If
' 値の存在チェック
Console.WriteLine(dict.ContainsValue("日本")) ' 出力:True
' 項目(キーと値)の列挙
For Each kvp As KeyValuePair(Of String, String) In dict
  Console.WriteLine("{0} : {1}", kvp.Key, kvp.Value)
Next
' ソート済みのハッシュテーブルの利用
Dim sdict As New SortedDictionary(Of string, string)(dict)
For Each kvp As KeyValuePair(Of String, String) In sdict
  Console.WriteLine("{0} : {1}", kvp.Key, kvp.Value)
Next

コメント

このブログの人気の投稿

Python OpenCVとWebカメラでバーコードリーダー

VB.net Dictionaryクラスの初期化