首頁  >  文章  >  後端開發  >  C# 程式檢查雜湊表中是否存在值

C# 程式檢查雜湊表中是否存在值

PHPz
PHPz轉載
2023-08-29 10:37:021354瀏覽

C# 程序检查哈希表中是否存在值

The hashtable is an organized collection of key-value pairs wherein the keys are arranged as per the hash code of the key calculated using the hash function. While the keys shull be non-n and unique in a hashtable, the values can be null and duplicate.

哈希表中的元素是透過鍵來存取的。在C#中,類別"Hashtable"表示雜湊表集合。這個類別提供了各種屬性和方法,我們可以使用它們來執行操作並存取哈希表中的資料。

在本文中,我們將看到如何確定雜湊表中是否存在特定的值。

How to Check if Value Exists in Hashtable?

#要檢查雜湊表中是否存在某個值,我們可以利用Hashtable類別提供的「containsValue」方法。此方法傳回布林值,指示指定的值是否存在於雜湊表中。

Let’s have a look at the method first before proceeding with programming examples.

ContainsValue方法

Syntax − public virtual bool ContainsValue (object value);

Description − used to find if the Hashtable contains a specified value.

參數 - 要在雜湊表中定位的值(物件)。可以是空值。

傳回值  Boolean: true=> 雜湊表中包含指定值的元素。

False=> 哈希表不包含指定值的元素。

命名空間 - System.Collections

#Let’s now see few programming examples where we check if the specified value is present in the hashtable or not.

Example

的中文翻譯為:

範例

檢查值是否存在於雜湊表中的第一個程式如下所示。

using System;
using System.Collections;
class Program {
   public static void Main(){
      // Create a Hashtable
      Hashtable langCodes = new Hashtable();
      
      // Add elements to the Hashtable
      langCodes.Add("C++", "CPlusPlus");
      langCodes.Add("C#", "CSharp");
      langCodes.Add("Java", "Java");
      langCodes.Add("PL", "Perl");
      
      // use ContainsValue method to check if the HashTable contains the 
      //required Value or not.
      if (langCodes.ContainsValue("CSharp"))
         Console.WriteLine("langCodes hashtable contain the Value = CSharp");
      else
         Console.WriteLine("langCodes hashtable doesn't contain the Value = CSharp");
   }
}

上述程式宣告了一個langCodes雜湊表,其中包含語言程式碼和語言名稱作為鍵和值。接下來,我們有一個「if」結構,檢查雜湊表中是否存在值「CSharp」。如果存在,它將相應地顯示訊息。

輸出

程式的輸出如下所示。

#
langCodes hashtable contain the Value = CSharp

由於hashtable中存在value = CSharp,所以程式會顯示上述訊息。

Now change the argument of the ContainsValue method to “C#” i.e. the key instead of value.

if (langCodes.ContainsValue("C#"))

現在用這個變更來執行上面的程式。

輸出

在這種情況下,由於雜湊表中不存在值“C#”,程式將傳回適當的訊息。因此,我們將得到−

#
langCodes hashtable doesn't contain the Value = CSharp

Example

的中文翻譯為:

範例

Now let’s look at the following example.

using System;
using System.Collections;
class Program {
   public static void Main() {
      // Create a Hashtable
      Hashtable NumberNames = new Hashtable();
      // Add elements to the Hashtable
      NumberNames.Add(1, "One");
      NumberNames.Add(3, "Three");
      NumberNames.Add(5, "Five");
      NumberNames.Add(7, "Seven");
      // use ContainsValue method to check if the HashTable contains the
      //required Value or not.
      if (NumberNames.ContainsValue("Two"))
         Console.WriteLine("NumberNames hashtable contain the Value = Two");
      else
         Console.WriteLine("NumberNames hashtable doesn't contain the Value = Two");
      if (NumberNames.ContainsValue("Five"))
         Console.WriteLine("NumberNames hashtable contain the Value = Five");
      else
         Console.WriteLine("NumberNames hashtable doesn't contain the Value = Five");
   }
}

這個程式有一個名為「NumberNames」的表,以數字作為鍵,對應的名稱作為值。在這裡,我們首先使用“containsKey()”方法檢查雜湊表是否包含值= Two。接下來,我們使用containsKey()方法檢查值=“Five”。 

Output

The output for the program is shown below.

#
NumberNames hashtable doesn't contain the Value = Two
NumberNames hashtable contain the Value = Five

從程式中定義的雜湊表中可以看出,它不包含值 = Two,但包含值 = Five。因此,程序適當地給出了相應的訊息。

結論

Thus using the “containsKey()” method of the Hashtable class in C#, we can determine if an element with a specific value is present in the hashtable or not. Depending on whether the value is present or not, we can output the appropriate results, or in the case of a complex program, proceed with the appropriate code.

當我們需要檢查hashtable中是否存在指定的值,並採取相應的行動時,方法「containsKey()」就變得非常有用。

以上是C# 程式檢查雜湊表中是否存在值的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除