总结
豆包 AI 助手文章总结

如何使用指定键从 C# 中的 HashTable 集合中获取值

王林
发布: 2023-08-28 23:09:06
转载
660人浏览过

如何使用指定键从 c# 中的 hashtable 集合中获取值

A hashtable is a collection of key−value pairs. We can access key−value pairs using an iterator. We can also access the keys of the hashtable in a collection. Similarly, we can access the values in a hashtable. Given a hashtable, it is also possible to access the value of a specified key or matching key of a specified value.

让我们讨论一下如何在给定键的哈希表集合中访问一个值。

如何使用指定的键从Hashtable集合中获取值?

Here, we have to obtain a value from the key−value pair of hashtables when a key is given.

考虑以下哈希表。

{“US", "New York"}
{"FR", "Paris"}
{"UK", "London"}
{"IN", "Mumbai"}
{"GER", "Berlin"}
登录后复制

Here, let’s suppose we have to find the value for the key “UK”. So we have to traverse the hashtable to find out if the hashtable contains the key = UK. Once the key=” UK” is found, we can access its corresponding value as hashtable[key].

Example

The program that exactly performs the above operation is shown below −

using System;
using System.Collections;
class MyHashTable {
   // Main Method
   static public void Main() {

      // Create a hashtable instance
      Hashtable Citytable = new Hashtable();

      // Adding key/value pair in the hashtable using Add() method
      Citytable.Add("US", "New York");
      Citytable.Add("FR", "Paris");
      Citytable.Add("UK", "London");
      Citytable.Add("IN", "Mumbai");
      Citytable.Add("GER", "Berlin");
      
      String key;
      Console.WriteLine("Enter the key whose value is to be printed:");
      key = Console.ReadLine();
      if(key != ""){
         if(Citytable.Contains(key) == true){
         string keyval = (string)Citytable[key];
         Console.WriteLine("The value of key {0} = {1}", key,keyval);
      }
      else
         Console.WriteLine ("Value for the key= {0} does not exist", key);
      }    
      Console.ReadKey();
   }
}
登录后复制

在上面的程序中,我们定义了一个哈希表。然后用户输入要获取值的键。一旦键被读取为输入,我们首先确定键是否为null或空。这是因为哈希表的键不应为null。因此,如果用户输入一个空值,我们将不会继续查找值。

因此,如果键不为空,我们将检查哈希表是否包含指定的键。为此,我们使用C#中的哈希表集合方法 Contains() ,如果键存在于哈希表中则返回true,如果键不存在则返回false。

如果 Contains() 方法返回 true,则我们只需访问该特定键的值。

string keyval = (string)Citytable[key];
登录后复制

Then this value is displayed to the user.

Output

Enter the key whose value is to be printed:
FR
The value of key FR = Paris
登录后复制

在这个输出中,用户执行了程序并输入了键值为FR。由于这个键已经存在于哈希表中,该键对应的值成功返回。

现在,如果我们输入一个在哈希表中不存在的键值?

Let’s execute the program again. Now we do not have a key in our hashtable for the country Canada. Let’s enter the key as CAN for Canada. The output is shown below.

Output

Enter the key whose value is to be printed:
CAN
Value for the key= CAN do not exist
登录后复制

在这里,由于哈希表中不包含 key=CAN,程序返回值不存在的消息。

以这种方式,我们可以开发一个交互式程序,从哈希表集合中找到指定键的值。

Let’s take another example to find the value given a key using a hashtable.

Here we will consider the following hashtable containing numbers and their corresponding number names.

{“1.1", "One point One"}
{"1.2", "One point Two"}
{"1.3", "One point Three"}
{"1.4", "One point Four"}
{"1.5", "One point Five"}
登录后复制

类似于前面的示例,这里我们也会要求用户输入要查找值的键,然后在哈希表中搜索指定的键并显示其值。

Example 2

Below given is the program to do that same.

using System;
using System.Collections;
class MyHashTable {
   // Main Method
   static public void Main() {

      // Create a hashtable instance
      Hashtable Numbernames = new Hashtable();

      // Adding key/value pair in the hashtable using Add() method
      Numbernames.Add("1.1", "One point One");
      Numbernames.Add("1.2", "One point Two");
      Numbernames.Add("1.3", "One point Three");
      Numbernames.Add("1.4", "One point Four");
      Numbernames.Add("1.5", "One point Five");

      String key = "1.4";
      if(key != ""){
          if(Numbernames.Contains(key) == true){
              string keyval = (string)Numbernames[key];
              if(keyval != "")
                 Console.WriteLine("The value of key {0} = {1}", key,keyval);
              else
                 Console.WriteLine("The value for key = {0} does not exist", key);
          }
          else
             Console.WriteLine ("The key= {0} does not exist in the NumberNames hashtable", key);
      }    
      Console.ReadKey();
   }
}
登录后复制

The program is the same as the previous example except for the hashtable and an extra condition we have specified to check for an empty value. This is because it can so happen that a specified key might be present in the hashtable, but its corresponding value might be empty. Secondly, we are not reading user input in this program, instead, we have directly used a key = “1.4” and we print out the value of this key. So we introduced one more check in this program. Hence this program now checks −

  • If the key specified is empty

  • If the key is not empty, the program checks if the hashtable contains the key.

  • If the hashtable contains the key, then it retrieves the value for the key. If the value is not empty, then the program displays the value.

  • If the value is empty, the appropriate message is displayed.

Output

The value of key 1.4 = One point Four
登录后复制

This output is generated when we specify a correct key that is present in the hashtable.

在本文中,我们看到了如何通过键从哈希表集合中获取值。我们还通过几个编程示例展示了不同的输出,以清晰地说明概念。在我们接下来的文章中,我们将继续讨论哈希表的相关主题。

以上就是如何使用指定键从 C# 中的 HashTable 集合中获取值的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号