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# 程序的详细内容。