如何在C#中向Hashtable集合新增項


我們已經討論了雜湊表的基礎知識。C#中的雜湊表集合用於儲存鍵值對,其中每個鍵值對都根據鍵的雜湊碼進行組織。此雜湊碼是使用雜湊碼函式計算的。在內部,雜湊表使用桶來儲存資料。桶只不過是雜湊表中元素的虛擬組。每個桶都與一個雜湊碼相關聯。

從程式設計角度來看,雜湊表類似於字典物件,但與字典物件不同的是,雜湊表可以儲存不同資料型別的物件。在效能方面,雜湊表的效能較低,因為雜湊表的元素是物件。因此,必須對物件的裝箱和拆箱操作才能從雜湊表中儲存和檢索值。

在本文中,讓我們討論如何向雜湊表集合新增項。

如何向Hashtable集合新增項?

C#中的Hashtable集合是使用Hashtable類實現的。此類提供各種方法來對雜湊表執行不同的操作。其中一種方法是Add()

Hashtable類的Add()方法用於在雜湊表中新增具有指定鍵及其對應值的元素。向雜湊表新增鍵值對時,應確保鍵不重複或為空,因為雜湊表只允許非空且唯一的鍵。

在C#的雜湊表集合中,我們可以有不同資料型別的鍵值對元素。

現在讓我們繼續學習Add()方法。

下面給出雜湊表集合的Add()方法的通用原型。

語法

public virtual void Add(object key, object value);

引數

  • Key - 要新增的元素的指定鍵(System.Object型別)。必須是非空的。

  • Value - 元素的指定值(System.Object型別)。此值可以為空。

異常:此方法丟擲以下異常。

  • ArgumentNullException - 當鍵為空時。

  • ArgumentException - 已存在具有相同鍵的元素。

  • NotSupportedException - Hashtable大小固定或為只讀。

如果我們聲明瞭一個如下所示的Hashtable物件:

Hashtable hshTable = new Hashtable();

然後,我們可以使用Add()方法向此Hashtable物件新增元素,如下所示:

hshTable.Add("msg", "charVal");

由於Hashtable允許混合資料型別的元素,因此我們也可以在同一個Hashtable中新增數值:

hshTable.Add(1, 2022);

除了使用Add()方法外,我們還可以直接將值賦值給Hashtable。例如,要新增一個鍵為2的元素,我們可以簡單地編寫:

hshTable[3] = "three";

以上語句將在雜湊表中建立一個鍵值對(3,“three”)。

向Hashtable集合新增項的程式設計示例

以下程式演示了Add()方法來構建一個包含不同元素的雜湊表。

示例1

using System;
using System.Collections;
class Program {
   static void Main(string[] args) {
      Hashtable mixedHashTable = new Hashtable();
      //add method
      mixedHashTable.Add("msg", "Collection");
      mixedHashTable.Add("site", "HashTable");
      mixedHashTable.Add(1, 3.14);
      mixedHashTable.Add(2, null);

      //assign value to the key
      mixedHashTable[3] = "Tutorial";

      // Add method throws an exception if the key already exists in //hashtable
      try {
         mixedHashTable.Add(2, 750);
      } catch {
         Console.WriteLine("Hashtable already has an element with Key = '2'.");
      }
      Console.WriteLine("*********HashTable Elements********");
      // It will return elements as Key-Value Pair.
      foreach (DictionaryEntry elem in mixedHashTable) {
         Console.WriteLine("Key = {0}, Value = {1}", elem.Key, elem.Value);
      }
      Console.ReadLine();
   }
}

上述程式首先使用預設建構函式建立一個Hashtable物件。然後,它使用Add()方法向雜湊表新增不同的元素。我們也可以透過直接賦值來向雜湊表新增元素。上述程式向雜湊表新增不同資料型別的鍵值對。然後,使用迭代器逐個顯示雜湊表的元素。

輸出

上述示例的輸出如下所示:

Hashtable already has an element with Key = '2'.
*********HashTable Elements********
Key = 2, Value = 
Key = msg, Value = Collection
Key = 3, Value = Tutorial
Key = site, Value = HashTable
Key = 1, Value = 3.14

輸出顯示了我們新增到雜湊表的所有鍵值對。

讓我們來看另一個向雜湊表新增元素的示例。程式如下所示。

示例2

using System;
using System.Collections;
class hTable {
   // Driver code
   public static void Main() {
      // Creating a Hashtable
      Hashtable strHashTable = new Hashtable();

      // Adding elements in Hashtable
      strHashTable.Add("4", "Even Number");
      strHashTable.Add("9", "Odd Number");
      strHashTable.Add("5", "Odd and Prime Number");
      strHashTable.Add("2", "Even and Prime Number");

      // Get a collection of the keys.
      ICollection c = strHashTable.Keys;

      // Displaying the hashtable contents
      Console.WriteLine("=========Contents of the Hashtable=========");
      foreach(string str in c)
         Console.WriteLine(str + ": " + strHashTable[str]);
   }
}

在這個程式中,我們正在新增字串型別的值。我們使用Add()方法新增值,然後檢索雜湊表中的鍵集合。使用foreach迴圈,我們遍歷此鍵集合並顯示每個鍵及其對應的值。

輸出

生成的輸出如下:

=========Contents of the Hashtable=========
5: Odd and Prime Number
9: Odd Number
2: Even and Prime Number
4: Even Number

透過這種方式,我們可以使用Hashtable類的Add()方法向Hashtable集合新增項。

在本文中,我們學習瞭如何向Hashtable集合新增項。在以後的文章中,我們將討論更多關於Hashtable的操作。

更新於:2022年12月14日

602 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.