首頁  >  文章  >  後端開發  >  如何在 C# 中建立 HashTable 集合?

如何在 C# 中建立 HashTable 集合?

PHPz
PHPz轉載
2023-08-31 23:13:02998瀏覽

如何在 C# 中创建 HashTable 集合?

HashTable是 C# 中的非泛型集合。它儲存鍵值對,類似於通用的“字典”集合。 HashTable 定義在

System. Collections. namespace.

HashTable計算每個鍵的雜湊碼並將其儲存在內部不同的桶中。然後,當存取值時,雜湊碼將與指定鍵的雜湊碼進行匹配。因此,查找透過 HashTable 進行了最佳化。

在本教程中,我們將了解如何在 C# 中建立 HashTable 集合。

哈希表功能

在我們開始建立 HashTable 之前,讓我們先看看 C# 中 HashTable 集合的一些顯著功能。

HashTable 集合儲存鍵值對。

哈希表是系統的一部分。 C# 中的集合命名空間並實作 IDictionary 介面。 HashTable 的元素儲存為 DictionaryEntry 物件。

哈希表的鍵不能為空且是唯一的。但是,值可以為空或重複。

可以使用索引器中的鍵來存取哈希表中的值,就像存取數組值一樣。

HashTable 中的鍵是不可變的物件。這些關鍵物件中的每一個都提供了一個雜湊函數。

典型的 Hashtable 類別實作 C# 的 IDictionary、ICollection、ISerializable、IEnumerable、IDeserializationCallback 和 ICloneable 介面。

HashTable中儲存的元素可以是相同類型,也可以是不同類型。

牢記這些顯著特徵,現在讓我們討論如何在 C# 中建立雜湊表。

如何在 C# 中建立 HashTable 集合?

C#的HashTable類別提供了16個重載建構子來建立HashTable。

下表顯示了我們將在本文中使用的 HashTable 建構子。

建構子 描述
哈希表() #初始化新的、具有預設初始容量、雜湊碼提供者、比較器和負載因子的 HashTable 類別的空實例。
哈希表(IDictionary) 建立 Hashtable 類別的新實例並使用指定字典的內容對其進行初始化。

注意- 要了解有關 C# 中 HashTable 類別的更多信息,請閱讀我們的文章 C#-HashTable 類別。

讓我們看看在 C# 中建立 HashTable 集合通常遵循的步驟。

首先,我們包括 System.我們程式中的集合命名空間

using System. Collections;

接下來,我們使用 Hashtable 類別來建立一個雜湊表。為此,我們使用預設建構函數。

Hashtable hashtable_name = new Hashtable();

現在我們可以使用「Add()」方法將元素加入HashTable。

所以這裡,我們可以在建立HashTable實例的同時初始化整個HashTable,也可以使用Add()方法將元素一一加入到HashTable中。

範例 1

以下程式示範了用 C# 建立 HashTable。

using System;
using System. Collections;
class MyHashTable {
   // Main Method
   static public void Main() {

      // Create hashtable using the default constructor
      Hashtable indianNumberSystem = new Hashtable();
      
      //add a key/value pair using the Add() method
      indianNumberSystem.Add(1,"Ones"); 
      indianNumberSystem.Add(10,"Tens");
      indianNumberSystem.Add(100,"Hundred");
      indianNumberSystem.Add(1000,"Thousand");
      indianNumberSystem.Add(10000,"Ten Thousand");
      indianNumberSystem.Add(100000,"Lac");
      indianNumberSystem.Add(1000000,"Ten Lac");
      indianNumberSystem.Add(10000000,"Crore");
      
      //display HashTable contents
      Console.WriteLine("Key, Value pairs from Indian Number System:");
      foreach(DictionaryEntry ele1 in indianNumberSystem){
         Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
      }      
   }
}

在上面的程式中,我們使用預設建構子定義了一個 HashTable 實例。然後我們使用 Add()方法將鍵/值對加到HashTable中。最後,使用 for-each 迴圈將 HashTable 內容一一列印出來。

輸出

上述程式產生以下輸出。

Key, Value pairs from Indian Number System:
100 (Hundred) 
1000 (Thousand) 
10 (Tens) 
1000000 (Ten Lac) 
100000 (Lac) 
10000000 (Crore) 
10000 (Ten Thousand) 
1 (Ones)

程式顯示一個包含印度數字系統的位元值的雜湊表。請注意,由於這是一個創建哈希表並向其中添加內容的簡單程序,因此輸出未格式化。

範例 2

讓我們再舉一個在 C# 中建立 HashTable 的範例。以下程式使用不同的建構子來建立 HashTable。

using System;
using System.Collections;
class MyHashTable {
   // Main Method
   static public void Main() {

      // Create hashtable without using Add method
      Hashtable my_hashtable1 = new Hashtable() {{"K1", "New York"}};

      // Adding key/value pair in the hashtable using Add() method
      my_hashtable1.Add("K2", "Paris");
      my_hashtable1.Add("K3", "London");
      my_hashtable1.Add("K4", "Mumbai");
      my_hashtable1.Add("K5", "Berlin");
      
      Console.WriteLine("Key, Value pairs from my_hashtable1:");
      foreach(DictionaryEntry ele1 in my_hashtable1){
         Console.WriteLine("{0} and {1} ", ele1.Key, ele1.Value);
      }      
   }
}

如我們在上面的程式碼中所看到的,首先我們建立一個具有一個鍵值對的 HashTable 物件。然後我們使用HashTable類別的add()方法來為HashTable新增元素。最後,使用 for-each 循環,迭代 HashTable 物件以列印每個 hashTable 元素(鍵值對)。

輸出

上面的程式產生以下輸出。

Key, Value pairs from my_hashtable1:
K2 and Paris 
K1 and New York 
K3 and London 
K4 and Mumbai 
K5 and Berlin 

在上面的輸出中,鍵值對按值的字母順序逆序顯示。這是哈希表的預設輸出,因為我們沒有提供任何程式碼來格式化輸出。 HashTable 類別提供了各種方法來組織/格式化輸出,我們將在後續教程中學習這些方法。

在本教學中,我們討論如何在 C# 中建立 HashTable 集合。 HashTable 是鍵值對的非通用集合。 HashTable 中的鍵是唯一的非空值。值可以為空和重複。我們可以使用Systems提供的HashTable類別在C#中建立HashTable。集合接口並使用此類提供的各種方法對其進行修改。

以上是如何在 C# 中建立 HashTable 集合?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除