如何使用指定鍵從 C# 中的 HashTable 集合中取得值

王林
發布: 2023-08-28 23:09:06
轉載
693 人瀏覽過

如何使用指定键从 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. This is because it can so happen that a specified key might be preent in the happen mbutent in key might 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 value. 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中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板