卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章18771本站已运行341

如何在 C# 中从哈希表集合中删除项目?

如何在 C# 中从哈希表集合中删除项目?

Hashtable在C#中是一个基于键的哈希码组织的键值对集合。可以使用键来访问hashtable中的项。C#的Hashtable类是实现hashtable的类。

使用此类,我们可以在提供的构造函数的帮助下创建一个新的哈希表对象。 Hashtable 类还提供了各种方法,我们可以使用它们对哈希表执行各种操作。这些操作包括添加项目、检查指定键是否存在、计算项目数量、从哈希表中删除项目等。

在本文中,我们将讨论从给定指定键的哈希表集合中删除项目。

如何从Hashtable集合中删除项目?

Hashtable类提供了一个名为“Remove”的方法,用于从hashtable集合中删除一个项。给定一个键,Remove方法将从hashtable中删除具有指定键的项。

下面给出了Remove方法的原型。

语法

'
public virtual void Remove (object key);

参数

  • Key − 要从哈希表集合中删除的元素的键。这是类型为 System.Object的。

实现

Remove(Object)方法是IDictionary接口的一部分。

异常

  • ArgumentNullException −如果指定的键为 null,则抛出此异常。

  • 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

在这里,用户输入了key = 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();
   }
}

在这个程序中,我们有一个哈希表,其中包含绕口令“她在海边卖贝壳”。我们将按键编号为 1a、1b、1c、2a、2b 等。首先,我们显示了整个哈希表。然后我们使用Remove()方法并删除key = 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()方法的全部内容,用于根据指定的键从哈希表集合中删除项。

卓越飞翔博客
上一篇: 在C/C++中,long long是一种数据类型,用于表示更大范围的整数。它通常占据8个字节的存储空间,并可以表示的整数范围更大,比普通的long类型更长
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏