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

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

将一个哈希表中的项目替换为另一个哈希表中的 C# 程序

将一个哈希表中的项目替换为另一个哈希表中的 C# 程序

C# 中的哈希表集合是基于键的哈希码组织的键值对的非泛型集合。键用于访问哈希表集合中的元素。散列可以帮助我们有效地检索数据并消除对昂贵的数据搜索技术的需要。散列技术使用密钥本身来定位数据。该哈希表键是不可变的,并且哈希表中不允许出现重复的条目。

Hashtable类定义在System.Collections命名空间中,为C#中的哈希表集合提供了基类库。这个Hashtable类用于创建一个使用哈希表进行存储的键值对集合。通过计算键的哈希码并将其存储在另一个篮子中,优化了对特定键的查找。当我们从哈希表中访问值时,它会将哈希码与指定的键进行匹配。

在本教程中,我们将讨论一种用另一个哈希表中的项或元素替换哈希表中的项或元素的方法。

如何用另一个Hashtable替换一个Hashtable中的项目?

上面讨论的Hashtable类提供了用于创建Hashtable对象的构造函数和用于添加、删除元素以及检查元素、键或值是否存在于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哈希表,并将indianNumberSystem哈希表的每个元素替换为langCodes哈希表的元素。

此程序的输出如下所示。

输出

'
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);
      }
   }
}

这里我们使用与第一个程序相同的方法,唯一的区别是第一个哈希表是空的。然后我们直接将第二个哈希表的项替换或移动到第一个哈希表中。

输出

The output of this program is given below.

'
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 的内容。

因此,通过使用简单的循环并遍历哈希表,我们可以用另一个哈希表替换其中的项目。

卓越飞翔博客
上一篇: 在C语言中,评估(Evaluation)、优先级(Precedence)和关联(Association)是什么?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏