本文介紹如何在Visual C# 中使用 HashTable
集合。
原始產品版本: Visual C#
原始 KB 編號: 309357
摘要
因為哈希不需要花費大量搜尋數據來擷取數據,因此您可以使用哈希來有效率地擷取數據。 哈希會使用索引鍵本身的值來找出數據。
基類庫提供 HashTable
命名空間中 System.Collections
定義的類別,因此您不需要撰寫自己的哈希表程序代碼。
建置範例的步驟
HashTable
集合會儲存 (Key
, Value
) 組,並使用 Key
來哈希並取得儲存位置。 Key
是不可變的,而且 在中HashTable
不能有重複的專案。 此範例會使用簡單 Person
類別的數個實例來儲存在 中 HashTable
。 姓氏會作為 Key
使用。
開啟 Microsoft Visual Studio,然後在 Visual C# 中建立 Windows Forms 應用程式專案。 Form1 預設會新增至專案。
在 方案總管 中,以滑鼠右鍵按兩下項目名稱,指向[新增],然後選取 [類別] 以新增類模組。
Class1
預設會新增至專案。以下欄程式代碼取代模組中的任何
Class1
程式代碼:public class Person { public string Fname, Lname;d public Person (string FirstName, string LastName) { Fname = FirstName; Lname = LastName; } public override string ToString () { return Fname + " " + Lname; } }
類別
Person
有一個建構函式會接受FirstName
和LastName
參數,並將這些參數指派給局部變數。 函ToString
式會ToString
從Object
類別覆寫,以傳回Fname
並Lname
串連在一起。建立窗體層級
Hashtable
物件,並宣告 類型的Person
三個變數。 將下列程式碼新增至Form1
類別:<?xm-deletion_mark author="v-bobbid" time="20080711T172143-0800" data="private Hashtable MyTable = new Hashtable(); //For simplicity, create three Person objects to add to the HashTable collection. Person Person1,Person2,Person3; "?> <?xm-insertion_mark_start author="v-bobbid" time="20080711T172143-0800"?> System.Collections.Hashtable MyTable = new System.Collections.Hashtable(); //For simplicity, create three Person objects to add to the HashTable collection. Person Person1,Person2,Person3; <?xm-insertion_mark_end?>
在下列步驟中,使用
Add
物件的 方法Hashtable
,將三Person
個try-catch
物件新增至Hashtable
區塊中的 。 區塊try-catch
會攔截例外狀況,並在有重複的索引鍵存在時顯示訊息:將 Button 控件放在 Form1 上,並將 Text 屬性變更為 [新增專案]。
按兩下按鈕以開啟其 [程式代碼] 視窗,並在 事件中貼上
Button1_Click
下列程式代碼:Person1 = new Person("David", "Burris"); Person2 = new Person("Johnny", "Carrol"); Person3 = new Person("Ji", "Jihuang"); //The Add method takes Key as the first parameter and Value as the second parameter. try { MyTable.Add(Person1.Lname, Person1); MyTable.Add(Person2.Lname, Person2); MyTable.Add(Person3.Lname, Person3); } catch (ArgumentException ae) { MessageBox.Show("Duplicate Key"); MessageBox.Show(ae.Message); }
物件
Hashtable
會提供索引器。 在下列步驟中,使用Key
來建立索引,以存取儲存在哈希位置的值:將 Button 控件新增至 Form1,並將 Name 屬性變更為 [取得專案]。
按兩下按鈕,並在事件中
Button2_Click
貼上下列程式代碼://Use the indexer of the Hashtable class to retrieve your objects. The indexer takes //Key as a parameter and accesses it with the Hashed location. try { MessageBox.Show(MyTable[Person1.Lname].ToString()); MessageBox.Show(MyTable[Person2.Lname].ToString()); MessageBox.Show(MyTable[Person3.Lname].ToString()); } catch (NullReferenceException ex) { MessageBox.Show("Key not in Hashtable"); MessageBox.Show(ex.Message); }
在下列步驟中
Remove
,使用 方法從集合中移除單一專案HashTable
:將 Button 控件新增至 Form1,並將 Text 屬性變更為 Remove Item。
按兩下按鈕,並在事件中
Button3_Click
貼上下列程式代碼:<?xm-deletion_mark author="v-bobbid" time="20080711T173011-0800" data="if (MyTable.Count == 0) { MessageBox.Show("There are no items in HashTable"); } else { MessageBox.Show("The count before removing an Item is" + " " + MyTable.Count); MessageBox.Show("Removing value stored at key value (Burris)"); Remove the object that is stored at the Key value Person1.Lname. MyTable.Remove(Person1.Lname); } "?> <?xm-insertion_mark_start author="v-bobbid" time="20080711T173011-0800"?>if (MyTable.Count == 0) { MessageBox.Show("There are no items in HashTable"); } else { MessageBox.Show("The count before removing an Item is" + " " + MyTable.Count); MessageBox.Show("Removing value stored at key value (Burris)"); // Remove the object that is stored at the Key value Person1.Lname. MyTable.Remove(Person1.Lname); } <?xm-insertion_mark_end?>
在下列步驟中,列舉儲存在集合中的
HashTable
專案:將 Button 控件新增至 Form1,並將 Text 屬性變更為 Enumerate。
按兩下按鈕,並在事件中
Button4_Click
貼上下列程式代碼:<?xm-deletion_mark author="v-bobbid" time="20080711T174252-0800" data="IDictionaryEnumerator Enumerator; if (MyTable.Count == 0) MessageBox.Show("The hashtable is empty"); else { MessageBox.Show("Enumerating through the Hashtable collection"); Enumerator = MyTable.GetEnumerator(); while (Enumerator.MoveNext()) { MessageBox.Show(Enumerator.Value.ToString()); } } ICollection MyKeys; if (MyTable.Count == 0) MessageBox.Show("The hashtable is empty"); else { MessageBox.Show("Accessing keys property to return keys collection"); MyKeys = MyTable.Keys; foreach (object Key in MyKeys) { MessageBox.Show(Key.ToString()); } } "?> <?xm-insertion_mark_start author="v-bobbid" time="20080711T174252-0800"?> System.Collections.IDictionaryEnumerator Enumerator; if (MyTable.Count == 0) MessageBox.Show("The hashtable is empty"); else { MessageBox.Show("Enumerating through the Hashtable collection"); Enumerator = MyTable.GetEnumerator(); while (Enumerator.MoveNext()) { MessageBox.Show(Enumerator.Value.ToString()); } } System.Collections.ICollection MyKeys; if (MyTable.Count == 0) MessageBox.Show("The hashtable is empty"); else { MessageBox.Show("Accessing keys property to return keys collection"); MyKeys = MyTable.Keys; foreach (object Key in MyKeys) { MessageBox.Show(Key.ToString()); } } <?xm-insertion_mark_end?>
此程式代碼會宣告 型
IDictionaryEnumerator
別的變數,並呼叫GetEnumerator
集合的HashTable
方法。 傳回 時Enumerator
,程式代碼會透過集合中的專案列舉,並使用Keys
的HashTable
方法來列舉索引鍵。
在下列步驟中, 使用
Clear
方法來清除HashTable
:將 Button 控件新增至 Form1,並將 Text 屬性變更為 Clear。
按兩下按鈕,並在事件中
Button5_Click
貼上下列程式代碼:MyTable.Clear(); MessageBox.Show("HashTable is now empty");
請遵循下列步驟來建置並執行應用程式:
- 選取 [ 新增專案]。 三
Person
個 物件會加入至HashTable
集合。 - 選取 [ 取得專案]。 索引器會取得集合中的
HashTable
專案。 隨即會顯示三個新加入的專案。 - 選取 [移除專案]。 刪除位於
Burris
索引鍵位置的專案。 - 選取 [ 列舉]。
IDictionaryEnumerator
列舉集合中的HashTable
專案,而Keys
的HashTable
屬性會傳回 Keys 集合。 - 選取 [清除]。 所有項目都會從
HashTable
集合中清除。
- 選取 [ 新增專案]。 三
注意
此處所描述的範例公司、組織、產品、網域名稱、電子郵件地址、商標、人員、地點與事件均屬虛構。 並非影射任何真實的公司、組織、產品、網域名稱、電子郵件地址、商標、人員、地點或事件。