集合——Dictionary



集合——Dictionary<K,V>

Dictionary 泛型類有兩個類型參數,而 List 中只有一個。Dirctionary<K,V>,其中 K 代表字典中鍵個類型,V 代表字典中值的類型。字典中用一個鍵對應一個值,鍵和值存在這種一一映射的關係,檢索速度取決於爲 K 指定的類型的哈希算法的質量。常用的方法與 List 類似,但是需要分別使用 Keys 屬性和 Values 屬性單獨枚舉鍵和值。

Dictionary 成員參見: msdn.microsoft.com/zh-cn/library/3eayzh46(VS.80).aspx

看下面的例子:

  1. using System;   
  2. using System.Collections.Generic;   
  3.   
  4. public class Example   
  5. {   
  6.     public static void Main()   
  7.      {   
  8.         // 使用 string 類型建立字典的鍵和值   
  9.          Dictionary<string, string> openWith =   
  10.             new Dictionary<string, string>();   
  11.   
  12.         // 添加一些元素給鍵和值,鍵是不可重複的,而值可以重複。   
  13.          openWith.Add("txt", "notepad.exe");   
  14.          openWith.Add("bmp", "paint.exe");   
  15.          openWith.Add("dib", "paint.exe");   
  16.          openWith.Add("rtf", "wordpad.exe");   
  17.   
  18.         // 捕獲字典中存在相同的鍵而產生的異常   
  19.         try  
  20.          {   
  21.              openWith.Add("txt", "winword.exe");   
  22.          }   
  23.         catch (ArgumentException)   
  24.          {   
  25.              Console.WriteLine("An element with Key = \"txt\" already exists.");   
  26.          }   
  27.   
  28.         // 可以使用鍵名作爲索引取出對應的值   
  29.          Console.WriteLine("For key = \"rtf\", value = {0}.",   
  30.              openWith["rtf"]);   
  31.   
  32.         // 也可以通過鍵名索引改變對應的值   
  33.          openWith["rtf"] = "winword.exe";   
  34.          Console.WriteLine("For key = \"rtf\", value = {0}.",   
  35.              openWith["rtf"]);   
  36.   
  37.         // 如果鍵名是不存在的,將會自動添加到字典   
  38.          openWith["doc"] = "winword.exe";   
  39.   
  40.         // 捕獲不存在的鍵名   
  41.         try  
  42.          {   
  43.              Console.WriteLine("For key = \"tif\", value = {0}.",   
  44.                  openWith["tif"]);   
  45.          }   
  46.         catch (KeyNotFoundException)   
  47.          {   
  48.              Console.WriteLine("Key = \"tif\" is not found.");   
  49.          }   
  50.   
  51.         // 用TryGetValue方法測試一個鍵是否存在,若存在就顯示出來   
  52.         string value = "";   
  53.         if (openWith.TryGetValue("tif", out value))   
  54.          {   
  55.              Console.WriteLine("For key = \"tif\", value = {0}.", value);   
  56.          }   
  57.         else  
  58.          {   
  59.              Console.WriteLine("Key = \"tif\" is not found.");   
  60.          }   
  61.   
  62.         // 通常我們在添加新鍵前使用ContainsKey方法   
  63.         if (!openWith.ContainsKey("ht"))   
  64.          {   
  65.              openWith.Add("ht", "hypertrm.exe");   
  66.              Console.WriteLine("Value added for key = \"ht\": {0}",   
  67.                  openWith["ht"]);   
  68.          }   
  69.   
  70.         // 使用foreach加KeyValuePair方法得到所有元素   
  71.          Console.WriteLine();   
  72.         foreach( KeyValuePair<string, string> kvp in openWith )   
  73.          {   
  74.              Console.WriteLine("Key = {0}, Value = {1}",   
  75.                  kvp.Key, kvp.Value);   
  76.          }   
  77.   
  78.         // 獲得值集合   
  79.          Dictionary<string, string>.ValueCollection valueColl =   
  80.              openWith.Values;   
  81.   
  82.         // 使用foreach枚舉所有值   
  83.          Console.WriteLine();   
  84.         foreach( string s in valueColl )   
  85.          {   
  86.              Console.WriteLine("Value = {0}", s);   
  87.          }   
  88.   
  89.         // 獲得鍵集合   
  90.          Dictionary<string, string>.KeyCollection keyColl =   
  91.              openWith.Keys;   
  92.   
  93.         // 使用foreach枚舉所有鍵   
  94.          Console.WriteLine();   
  95.         foreach( string s in keyColl )   
  96.          {   
  97.              Console.WriteLine("Key = {0}", s);   
  98.          }   
  99.   
  100.         // Remove方法刪除元素   
  101.          Console.WriteLine("\nRemove(\"doc\")");   
  102.          openWith.Remove("doc");   
  103.   
  104.         if (!openWith.ContainsKey("doc"))   
  105.          {   
  106.              Console.WriteLine("Key \"doc\" is not found.");   
  107.          }   
  108.   
  109.          Console.Write("\nPress any key to exit...");   
  110.          Console.ReadKey();   
  111.      }   
  112. }   
  113.   
  114. /* 運行結果:
  115. An element with Key = "txt" already exists.
  116. For key = "rtf", value = wordpad.exe.
  117. For key = "rtf", value = winword.exe.
  118. Key = "tif" is not found.
  119. Key = "tif" is not found.
  120. Value added for key = "ht": hypertrm.exe
  121. Key = txt, Value = notepad.exe
  122. Key = bmp, Value = paint.exe
  123. Key = dib, Value = paint.exe
  124. Key = rtf, Value = winword.exe
  125. Key = doc, Value = winword.exe
  126. Key = ht, Value = hypertrm.exe
  127. Value = notepad.exe
  128. Value = paint.exe
  129. Value = paint.exe
  130. Value = winword.exe
  131. Value = winword.exe
  132. Value = hypertrm.exe
  133. Key = txt
  134. Key = bmp
  135. Key = dib
  136. Key = rtf
  137. Key = doc
  138. Key = ht
  139. Remove("doc")
  140. Key "doc" is not found.
  141. */  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章