C#程式替換一個雜湊表中的專案為另一個雜湊表


C# 中的雜湊表集合是一個非泛型集合,包含鍵值對,這些鍵值對根據鍵的雜湊碼進行組織。鍵用於訪問雜湊表集合中的元素。雜湊幫助我們有效地檢索資料,並避免了代價高昂的資料搜尋技術。雜湊技術使用鍵本身來定位資料。這個雜湊表鍵是不可變的,並且不允許在雜湊表中出現重複項。

Hashtable 類在 System.Collections 名稱空間中定義,為 C# 中的雜湊表集合提供了基礎類庫。此 Hashtable 類用於建立使用雜湊表進行儲存的鍵值對集合。透過計算鍵的雜湊碼並將其儲存在另一個內部籃子中,可以最佳化對特定鍵的查詢。當我們從雜湊表訪問值時,它會將雜湊碼與指定的鍵進行匹配。

在本教程中,我們將討論一種用另一個雜湊表替換一個雜湊表中的專案或元素的方法。

如何用另一個雜湊表替換一個雜湊表中的專案?

上面討論的 Hashtable 類提供了用於建立 Hashtable 物件的建構函式以及用於新增、刪除元素以及檢查元素或鍵或值是否出現在雜湊表中的方法。它還提供了一些屬性來檢查雜湊表是否為空、計算雜湊表中的元素數量等。

但它不允許我們的方法用另一個雜湊表替換整個雜湊表中的專案。我們可以透過替換其值來替換單個專案。

要將整個雜湊表內容替換為另一個雜湊表的內容,我們通常會遍歷第二個雜湊表,並將第一個雜湊表的內容替換為第二個雜湊表的內容。

我們將使用下面所示的方法。

foreach (DictionaryEntry item in secondHashtable) {
   firstHashtable[item.Key] = item.Value;
   Console.WriteLine("{0} ({1}) ", item.Key, item.Value);
}

這裡我們首先遍歷第二個雜湊表,然後用第二個雜湊表的每個鍵值對替換第一個雜湊表的每個鍵值對。

示例

實現此方法的完整程式如下所示。

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");
      Console.WriteLine("Contents of indianNumberSystem hashtable:");
      foreach(DictionaryEntry ele1 in indianNumberSystem){
         Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
      }
      Hashtable langCodes = new Hashtable();
      langCodes.Add("C++","CPlusPlus");
      langCodes.Add("C#","CSharp");
      langCodes.Add("Java","Java");
      langCodes.Add("PL","Perl");
      Console.WriteLine("
Contents of langCodes Hashtable:"); foreach(DictionaryEntry ele1 in langCodes){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } Console.WriteLine("
After Replacing with langCodes, indianNumberSystem: "); foreach (DictionaryEntry item in langCodes) { indianNumberSystem[item.Key] = item.Value; Console.WriteLine("{0} ({1}) ", item.Key, item.Value); } } }

這裡我們有兩個雜湊表,indianNumberSystem 和 langCodes。我們用值填充這兩個雜湊表,然後顯示其內容。然後我們遍歷 langCodes 雜湊表,並用 langCodes 雜湊表的每個元素替換 indianNumberSystem 雜湊表的每個元素。

此程式的輸出如下所示。

輸出

Contents of indianNumberSystem hashtable:
1000 (Thousand) 
10 (Tens) 
100 (Hundred) 
1 (Ones) 
Contents of langCodes Hashtable:
Java (Java) 
C# (CSharp) 
PL (Perl) 
C++ (CPlusPlus) 
After Replacing with langCodes, indianNumberSystem: 
Java (Java) 
C# (CSharp) 
PL (Perl) 
C++ (CPlusPlus) 

從輸出中我們可以看到,替換後 indianNumberSystem 的內容被 langCodes 的內容替換。

示例

現在讓我們看看下一個示例。

在這個示例中,我們仍然有兩個雜湊表,indianNumberSystem 和 numSys。這裡我們不填充雜湊表 indianNumberSystem。我們只是建立一個物件。numSys 雜湊表使用 Add 方法添加了以下值。

1

個位

10

十位

100

百位

1000

千位

此示例的完整程式如下所示。

using System;
using System. Collections;
class MyHashTable {
   // Main Method
   static public void Main() {
      // Create hashtable using the default constructor
      Hashtable indianNumberSystem = new Hashtable();
      Hashtable numSys = new Hashtable();
      numSys.Add(1,"Ones");
      numSys.Add(10,"Tens");
      numSys.Add(100,"Hundred");
      numSys.Add(1000,"Thousand");
      Console.WriteLine("
Contents of numSys Hashtable:"); foreach(DictionaryEntry ele1 in numSys){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } Console.WriteLine("
After Replacing with numSys, indianNumberSystem: "); foreach (DictionaryEntry item in numSys) { indianNumberSystem[item.Key] = item.Value; Console.WriteLine("{0} ({1}) ", item.Key, item.Value); } } }

這裡我們使用與第一個程式相同的方法,唯一的區別是第一個雜湊表為空。然後我們直接替換或移動第二個雜湊表中的專案到第一個雜湊表中。

輸出

此程式的輸出如下所示。

Contents of numSys Hashtable:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)
After Replacing with numSys, indianNumberSystem:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)

如程式輸出所示,numSys 表的內容現在是 indianNumberSystem 的內容。

因此,使用簡單的迴圈並遍歷雜湊表,我們可以用另一個雜湊表替換一個雜湊表中的專案。

更新於: 2022年12月22日

322 次瀏覽

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.