如何在 C# 中從雜湊表集合中刪除項?


C# 中的雜湊表是鍵值對的集合,這些鍵值對根據鍵的雜湊碼進行組織。雜湊表中的項使用鍵進行訪問。C# 的 Hashtable 類是實現雜湊表的類。

使用此類,我們可以藉助提供的建構函式建立新的雜湊表物件。Hashtable 類還提供各種方法,我們可以使用這些方法對雜湊表執行各種操作。這些操作包括新增項、檢查指定鍵是否存在、計數項數、從雜湊表中刪除項等等。

在本文中,我們將討論從給定指定鍵的雜湊表集合中刪除項。

如何從雜湊表集合中刪除項?

Hashtable 類提供了一個名為“Remove”的方法來從雜湊表集合中刪除/移除項。給定一個鍵,Remove 方法將從雜湊表中刪除具有指定鍵的項。

Remove 方法的原型如下所示。

語法

public virtual void Remove (object key);

引數

  • Key - 要從雜湊表集合中刪除的元素的鍵。其型別為 System.Object。

實現

IDictionary 介面的 Remove(Object) 方法。

異常

  • ArgumentNullException - 如果指定的鍵為空,則丟擲此異常。

  • NotSupportedException - 如果雜湊表大小固定或為只讀,則丟擲。

如果雜湊表中不存在指定的鍵,“Remove()”方法不會丟擲任何異常。如果鍵不存在,雜湊表將保持不變,程式將成功執行。

Remove() 方法的程式設計示例

示例 1

以下程式演示瞭如何使用 Remove() 方法從雜湊表集合中刪除項。

using System; using System.Collections; class MyHashTable { public static void Main(){ // Creating a Hashtable Hashtable numberNames = new Hashtable(); // Adding elements in Hashtable numberNames.Add("2", "Two"); numberNames.Add("13", "Thirteen"); numberNames.Add("24", "Twenty Four"); numberNames.Add("59", "Fifty Nine"); // Print the contents of Hashtable Console.WriteLine("**********Contents of Hashtable**********"); foreach(var item in numberNames.Keys){ Console.WriteLine("key ={0}, Value = {1}", item,numberNames[item]); } //read the key for which element is to be deleted Console.WriteLine("
Enter the key for which the element is to be removed from Hashtable "
); string key = (string)Console.ReadLine(); //remove the element numberNames.Remove(key); //display the hashtable after deletion Console.WriteLine("
******Contents of Hashtable(after deletion)******"
); foreach(var item in numberNames.Keys){ Console.WriteLine("key ={0}, Value = {1}", item,numberNames[item]); } } }

在此程式中,首先,我們建立了一個雜湊表,其中數字作為鍵,其對應的數字名稱作為值。然後在螢幕上顯示雜湊表。接下來,提示使用者輸入要從雜湊表中刪除其元素的鍵。輸入鍵後,將使用此鍵作為引數呼叫 Remove() 方法。接下來,再次顯示雜湊表內容。如果雜湊表中存在該鍵,則 Remove() 方法將刪除該元素,否則,雜湊表將保持不變。

輸出

程式生成以下輸出。

**********Contents of Hashtable**********
key =59, Value = Fifty Nine
key =24, Value = Twenty Four
key =13, Value = Thirteen
key =2, Value = Two

Enter the key for which the element is to be removed from Hashtable 
13
******Contents of Hashtable(after deletion)******
key =59, Value = Fifty Nine
key =24, Value = Twenty Four
key =2, Value = Two

以上輸出顯示了刪除前後雜湊表內容之間的差異。

現在讓我們看看當用戶輸入的鍵在雜湊表中不存在時輸出如何變化。在這種情況下,如前所述,雜湊表保持不變,並且不會丟擲任何異常。以下是上述程式生成的輸出。

輸出

**********Contents of Hashtable**********
key =59, Value = Fifty Nine
key =24, Value = Twenty Four
key =13, Value = Thirteen
key =2, Value = Two

Enter the key for which the element is to be removed from Hashtable 
3
******Contents of Hashtable(after deletion)******
key =59, Value = Fifty Nine
key =24, Value = Twenty Four
key =13, Value = Thirteen
key =2, Value = Two

這裡,使用者輸入了鍵 = 3,該鍵在雜湊表中不存在。在這種情況下,雜湊表保持不變,因為 Remove() 方法沒有刪除任何元素。

示例 2

現在讓我們考慮另一個雜湊表刪除的示例。該程式如下所示。

using System; using System.Collections; public class myHashtable{ public static void Main(){ // Create a new Hashtable. var tongueTwister = new Hashtable(); tongueTwister.Add("1a", "She"); tongueTwister.Add("1b", "sells"); tongueTwister.Add("1c", "sea"); tongueTwister.Add("2a", "shells"); tongueTwister.Add("2b", "on"); tongueTwister.Add("2c", "the"); tongueTwister.Add("3a", "sea"); tongueTwister.Add("3b", "shore"); // Displays the Hashtable. Console.WriteLine("The Hashtable initially contains the following:
"
); foreach (DictionaryEntry elem in tongueTwister) Console.WriteLine($" {elem.Key}: {elem.Value}"); Console.WriteLine(); // Removes the element with the specified key. string key =3b”; tongueTwister.Remove(key); // Displays the Hashtable after deletion. Console.WriteLine("
Hashtable after removing the key = "
{0}":",key); foreach (DictionaryEntry elem in tonguetwister) Console.WriteLine($" {elem.Key}: {elem.Value}"); Console.WriteLine(); } }

在此程式中,我們有一個雜湊表,其中包含繞口令“She sells sea shells on the seashore”。我們已將鍵編號為 1a、1b、1c、2a、2b 等。首先,我們顯示了整個雜湊表。然後我們使用 Remove() 方法並刪除鍵 = 3b 的元素。再次顯示新更新的雜湊表。

輸出

程式生成以下輸出。

The Hashtable initially contains the following:
    3b:    shore
    1a:    She
    1b:    sells
    2b:    on
    2c:    the
    3a:    sea
    2a:    shells
    1c:    sea

Hashtable after removing the key =  "3b":
    1a:    She
    1b:    sells
    2b:    on
    2c:    the
    3a:    sea
    2a:    shells
    1c:    sea
Note the hashtable after deleting the key = 3b.

Hashtable 類的 Remove() 方法用於一次刪除或移除雜湊表中的元素。如果雜湊表中不存在指定的鍵(方法的引數),Remove() 方法不會丟擲異常。它將簡單地繼續執行程式,而不會更改雜湊表。

這就是關於給定指定鍵從雜湊表集合中刪除項的 Remove() 方法的所有內容。

更新於: 2022年12月14日

330 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.