VB.net Dictionary型

ハッシュテーブル(連想配列)を使うには?より。
覚書
    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カメラでバーコードリーダー

OpenCV 画像の足し算

OpenCV3とPython3で形状のある物体の輪郭と方向を認識する(主成分分析:PCA、固有ベクトル)