C# 哈希表

WBOY
發布: 2024-09-03 15:04:59
原創
430 人瀏覽過

C# 中的雜湊表是鍵值對形式的資料集合,鍵值對基於鍵的雜湊碼,鍵用於存取集合內的元素或資料。它是從Objects類別繼承到Hashtable的。因此,基本上,C# 或任何程式語言中的雜湊表都是鍵和值對的簡單表示,它們以雜湊碼格式正確組織。

文法:

現在我們知道什麼是 C# 雜湊表,讓我們繼續了解正確實作雜湊表的標準語法。以下是在程式中使用雜湊表的標準語法和所需的系統檔案。

using System.Collections;
Hashtableht = new Hashtable();
登入後複製

包含集合的系統檔案負責匯入雜湊表使用的所需函數和方法。那麼hashtable就是這裡的主要關鍵字,我們建立了一個實例作為ht,我們的操作將會在新建立的ht上執行。現在我們知道了實作哈希表的正確語法,讓我們了解它是如何運作的。

哈希表在 C# 中如何運作?

如前所述,我們知道哈希表是鍵值對形式的資料或資訊的集合。鍵值對的一個簡單範例是“Name:Sulaksh”,這裡鍵是Name,值是Sulaksh,鍵保持不變,而值可以不同。 Hashtable 由 key 和 value 組成,以大括號表示,並使用雜湊函數進行計算。

現在讓我們正確實作雜湊表並了解範例的工作原理。

C# 哈希表示例

我們的第一個範例是哈希表的簡單實現,其中我們有一個簡單的哈希表,帶有數字鍵和值,我們將列印哈希表中的元素總數,程式碼如下:

範例#1

代碼:

using System;
using System.Collections;
class sampleht {
static public void Main() {
Hashtableexampleht = new Hashtable();
exampleht.Add(1, " Element 1");
exampleht.Add(2, " Element 2");
exampleht.Add(3, " Element 3");
Console.WriteLine("\n The Total No. of elements: {0}", exampleht.Count);
}
}
登入後複製

程式碼說明:從系統檔案開始,這裡集合是最重要的,然後是我們的類,其中是我們的 main 方法。在 main 方法中,我們宣告了一個雜湊表,後面跟著三個鍵值對。我們已經實作了 add 函數來插入元素。因此,我們的哈希表由三個鍵值對組成,最後,我們有一個列印語句,它將列印哈希表中的元素總數,即三個。我們在這裡使用一個簡單的計數函數,請參考下面所附的輸出螢幕截圖:

C# 哈希表

正如預期的那樣,輸出告訴我們哈希表中有四個元素,現在轉到下一個範例,我們將嘗試顯示哈希表的鍵和值。

範例#2

代碼:

using System;
using System.Collections;
class exampleHT {
static publicvoid Main() {
HashtablesampleHT = new Hashtable();
sampleHT.Add(1, " One");
sampleHT.Add(2, " Two");
sampleHT.Add(3, " Three");
Console.WriteLine("\n Below is the content of Hashtable: \n");
foreach (DictionaryEntry entry in sampleHT) {
Console.WriteLine(" {0}, {1}", entry.Key, entry.Value);
}
}
}
登入後複製

程式碼說明:與前面的範例類似,我們有系統檔案和類,其中包含 main 方法。然後我們有哈希表,後面是鍵值對,然後是列印語句。然後我們有 foreach 語句,它將一次選擇一個元素,並在下一行中將其列印為輸出。我們的輸出預計是鍵和值形式的元素列表,請參閱下面的螢幕截圖。

C# 哈希表

如預期的那樣,輸出列印了雜湊表的元素,現在在下一個範例中,我們將使用 outhashtable 實作清除函數,程式碼如下。

範例#3

代碼:

using System;
using System.Collections;
class sampleht {
static public void Main()  {
Hashtableexampleht = new Hashtable();
exampleht.Add(1, " Element 1");
exampleht.Add(2, " Element 2");
exampleht.Add(3, " Element 3");
Console.WriteLine("\n Elements of the Hashtable :");
foreach(DictionaryEntry ele1 in exampleht) {
Console.WriteLine(ele1.Value);
}
Console.WriteLine(" No. of elements before clearing: {0} ", exampleht.Count);
exampleht.Clear();
Console.WriteLine(" Total No. of elements now: {0} ", exampleht.Count);
}
}
登入後複製

程式碼說明:透過我們所需的系統檔案和方法,我們定義了雜湊表和總共三個鍵值對。我們已經實作了 add 函數來將元素新增到雜湊表中,然後我們的 print 語句將簡單地列印雜湊表中的所有元素,即三個。在清除哈希表之前,我們已經列印了列表中存在的元素總數,然後我們有clear函數,它將清除整個哈希表,這意味著每個元素都將從列表中清除,最後的列印語句將列印數字現在存在的元素數量將為零。

C# 哈希表

正如所解釋的,clear 函數完成了它的工作,並且列表被清除,現在,繼續,對於我們的下一個範例,我們將實現刪除函數。

範例#4

代碼:

using System;
using System.Collections;
class sampleht {
static public void Main() {
Hashtableexampleht = new Hashtable();
exampleht.Add(1, " Element 1");
exampleht.Add(2, " Element 2");
exampleht.Add(3, " Element 3");
Console.WriteLine("\n List of Original Elements:");
foreach (var key in exampleht.Keys )
Console.WriteLine(" {0}, {1}",key , exampleht[key]);
exampleht.Remove(3);
exampleht.Remove(1);
Console.WriteLine("\n Elements after removal:");
foreach (var key in exampleht.Keys )
Console.WriteLine(" {0}, {1}",key , exampleht[key]);
}
}
登入後複製

Code Explanation: Just as our earlier examples, we have our system files, class, and the main method. Then we have our hashtable with a total of three key values. Then we have our print statement, which will print out the original list along with key and values, using foreach. We then have the remove function for two keys, which will remove the keys that we pass, then we have our print statement, which will use foreach and print every single element present in the hashtable after removing.

C# 哈希表

Advantages

Advantages of any function or methodology is important to understand its real time applications and hashtable’s most widely known advantage is how it allows the developer the ability for synchronization. Hashtable has noted advantages over the search tree and this makes them practical to use in real time computer applications and for the purpose of database indexing. Speed, compared to other table type data structures is best delivered by hashtables. Having key value pairs makes it easy to anticipate the format regarding data and restructuring is done easily.

Conclusion

Hashtable in C# is a collection of elements, represented in a key value pair format. The key can be the same, while values differ and the key cannot be null while a value can be. We implemented an example of functions like remove, clear, and print. Hashtable has a speed advantage over other data structures.

以上是C# 哈希表的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!