合併兩個哈希表集合的 C# 程序
C# 中的雜湊表集合儲存鍵值對。該集合中的每個元素或項目都是鍵值對,即該集合是一個雙元素集合。 Key 是唯一的、非空的,用於存取哈希表中的元素。
哈希表集合是不可變的,不能有重複的元素。這意味著鍵值組合應該是唯一的。但是,這些值可以為空或重複。 .Net Framework 提供了一個 HashTable 類別來實作雜湊表集合,並包含實作雜湊表所需的功能,而無需任何額外的編碼。
哈希表集合中的每個元素都是一個具有兩個屬性的 DictionaryEntry 物件:鍵元素和值元素。當將元素新增至雜湊表時,會自動產生雜湊碼。該哈希碼是內部的並且是隱藏的。哈希表集合中的元素按隱藏雜湊碼排序。因此,哈希表元素被認為是隨機選擇的。
透過對雜湊表集合的簡要介紹,讓我們看看如何合併兩個雜湊表集合。
如何合併兩個哈希表集合?
Hashtable 類別由 System 提供。集合命名空間僅包含可用於建構雜湊表物件並執行新增/刪除元素、計算元素數量等操作的基底類別庫。沒有提供可用於將兩個哈希表合併在一起的方法/函數。
我們必須設計自己的方法來合併兩個雜湊表。我們知道哈希表的容量或大小是哈希表保存的元素數量。當元素插入哈希表時,哈希表的大小會透過重新分配自動增長。
因此,當我們將兩個雜湊表合併在一起時,我們會將一個雜湊表的元素加入到另一個雜湊表中。當我們新增元素時,該哈希表的大小將會相應調整。
方法
建立兩個哈希表物件。
使用 Add 方法用元素填滿兩個表。
-
使用鍵遍歷第二個雜湊表,如果當前項目(正在遍歷的鍵)尚不存在於第一個雜湊表中,則將其每個鍵值對新增至第一個哈希表中。
李> 列印產生的雜湊表。
注意:在新增鍵之前,我們會明確檢查該鍵是否存在於雜湊表中,因為雜湊表不允許新增重複鍵。
範例
將上述方法轉換為如下所示的 C# 程式。
using System; using System. Collections; class MyHashTable { static public void Main() { Hashtable indianNumberSystem = new Hashtable(); 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 indianNumberSystem){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } foreach (DictionaryEntry entry in langCodes) { if(!indianNumberSystem.ContainsKey(entry.Key)) { indianNumberSystem.Add(entry.Key, entry.Value); }} Console.WriteLine("Key, Value pairs after merging langCodes to indianNumberSystem:"); foreach(DictionaryEntry ele1 in indianNumberSystem){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } } }
這裡我們有兩個哈希表,就是 indianNumberSystem 和 langCodes。
哈希表 indianNumberSystem 具有以下數據,
1 |
「一個」 |
#10 | #「十」 |
#100 | #「一百」 |
#1000 | #「千」 |
#哈希表 langCodes 具有以下資料。
C |
「CPlusPlus」 |
#C | #「CSharp」 |
#Java |
“Java” |
#PL |
「Perl」 |
#我們先顯示這兩個表的內容。然後我們使用 langCodes 哈希表的鍵來遍歷它。在遍歷循環中,我們首先檢查哈希表 indianNumberSystem 是否具有相同的鍵。如果該鍵不存在,我們將目前鍵指向的 langCodes 元素新增至 indianNumberSystem 哈希表中。
輸出
最後,我們顯示合併後的表。
Contents of indianNumberSystem hashtable: 1000 (Thousand) 10 (Tens) 100 (Hundred) 1 (Ones) Contents of langCodes Hashtable: 1000 (Thousand) 10 (Tens) 100 (Hundred) 1 (Ones) Key, Value pairs after merging langCodes to indianNumberSystem: 100 (Hundred) 1000 (Thousand) PL (Perl) 10 (Tens) C# (CSharp) Java (Java) C++ (CPlusPlus) 1 (Ones)
從產生的輸出我們可以看到兩個表都正確合併。
範例
現在讓我們考慮另一個範例,即下面給出的 C# 程式。
using System; using System. Collections; using System.Collections.Generic; class MyHashTable { static public void Main() { Hashtable indianNumberSystem = new Hashtable(); 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 NumberNames = new Hashtable(); NumberNames.Add(1,"One"); NumberNames.Add(2,"Two"); NumberNames.Add(3,"Three"); NumberNames.Add(4,"Four"); Console.WriteLine("Contents of NumberNames Hashtable:"); foreach(DictionaryEntry ele1 in NumberNames){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } foreach (DictionaryEntry entry in NumberNames) { if(!indianNumberSystem.ContainsKey(entry.Key)) { indianNumberSystem.Add(entry.Key, entry.Value); }} Console.WriteLine("Key, Value pairs after merging NumberNames to indianNumberSystem:"); foreach(DictionaryEntry ele1 in indianNumberSystem){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } } }
該程式與前一個程式相同,只是我們用 NumberNames 雜湊表取代了 langCodes 雜湊表。 NumberNames 哈希表具有以下元素。
1 |
「一」 |
#2 |
「兩個」 |
#3 |
「三 |
4 |
「四」 |
#輸出
如我們所見,哈希表 indianNumberSystem 和 NumberNames 具有共同的資料。現在讓我們執行這個程式來檢查合併是如何發生的。
Contents of indianNumberSystem hashtable: 1000 (Thousand) 10 (Tens) 100 (Hundred) 1 (Ones) Contents of NumberNames Hashtable: 4 (Four) 3 (Three) 2 (Two) 1 (One) Key, Value pairs after merging NumberNames to indianNumberSystem: 100 (Hundred) 1000 (Thousand) 10 (Tens) 4 (Four) 3 (Three) 2 (Two) 1 (Ones)
從上面的輸出可以看出,NumberNames 中的資料元素 (key=1) 並沒有加入到 indianNumberSystem 哈希表中。這是因為不允許重複。
結論
因此,我們可以透過將一個哈希表的資料複製或新增到另一個哈希表集合來合併兩個哈希表集合。每當兩個哈希表中存在公共鍵時,就不會添加重複的鍵。但程式設計師必須確保在添加一個哈希表的數據時進行檢查,以免意外添加數據,從而導致不可預測的結果。
以上是合併兩個哈希表集合的 C# 程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#在企業級應用、遊戲開發、移動應用和Web開發中均有廣泛應用。 1)在企業級應用中,C#常用於ASP.NETCore開發WebAPI。 2)在遊戲開發中,C#與Unity引擎結合,實現角色控制等功能。 3)C#支持多態性和異步編程,提高代碼靈活性和應用性能。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#和.NET通過不斷的更新和優化,適應了新興技術的需求。 1)C#9.0和.NET5引入了記錄類型和性能優化。 2).NETCore增強了雲原生和容器化支持。 3)ASP.NETCore與現代Web技術集成。 4)ML.NET支持機器學習和人工智能。 5)異步編程和最佳實踐提升了性能。

C#在.NET中的編程過程包括以下步驟:1)編寫C#代碼,2)編譯為中間語言(IL),3)由.NET運行時(CLR)執行。 C#在.NET中的優勢在於其現代化語法、強大的類型系統和與.NET框架的緊密集成,適用於從桌面應用到Web服務的各種開發場景。

如何將C#.NET應用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。 1.在Azure上,使用AzureAppService和AzurePipelines自動化部署。 2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda實現部署和無服務器計算。

C#和.NET運行時緊密合作,賦予開發者高效、強大且跨平台的開發能力。 1)C#是一種類型安全且面向對象的編程語言,旨在與.NET框架無縫集成。 2).NET運行時管理C#代碼的執行,提供垃圾回收、類型安全等服務,確保高效和跨平台運行。
