欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

輕松學(xué)習(xí)C#的哈希表

 更新時(shí)間:2022年05月06日 14:18:16   作者:丿木呈廣予口貝  
輕松學(xué)習(xí)C#的哈希表,對C#的哈希表感興趣的朋友可以參考本篇文章,幫助大家更靈活的運(yùn)用C#的哈希表

在C#語言中,還有一種用于快速搜索而組織的鍵/值組合的數(shù)組,這種數(shù)組叫做關(guān)聯(lián)數(shù)組,也叫做哈希表(Hashtable)。

哈希表也在System.Collection命名空間下,用于處理和表現(xiàn)類似key/value的鍵值對,其中key通常用來快速查找,同時(shí)key是區(qū)分大小寫,且key必須是唯一的。它沒有有效的排序,所進(jìn)行的是內(nèi)在的排序,value用于存儲(chǔ)對應(yīng)于key的值。哈希表中key/value鍵值對均為object類型,所以哈希表可以支持任何類型的key/value鍵值對。哈希表的每個(gè)元素是一個(gè)存儲(chǔ)在DictionaryEntry對象中的鍵值對鍵值對(所謂的DictionaryEntry結(jié)構(gòu),就是定義可設(shè)置或檢索的字典鍵值對,有一個(gè)key屬性,一個(gè)value屬性,分別代表鍵和值)。

哈希表最大的優(yōu)點(diǎn)就是把數(shù)據(jù)的存儲(chǔ)和查找消耗的時(shí)間大大降低,幾乎可以看成常數(shù)時(shí)間,而代價(jià)僅僅是消耗較多的內(nèi)存。然而在當(dāng)前可利用內(nèi)存越來越多的情況下,用空間換時(shí)間的做法是值得的。另外,編碼比較容易也是它的特點(diǎn)之一。

一、Hashtable元素的添加

Hashtable提供了一個(gè)添加元素的key/value鍵值對Add方法,該方法有兩個(gè)參數(shù),一個(gè)是鍵,功能相當(dāng)于數(shù)組中的索引,幫助查找,另一個(gè)是值,可以把它看做數(shù)組中的元素,其格式為:Hashtable對象.Add(鍵,值)

例一、利用上述的方法進(jìn)行Hashtable對象的元素的添加

using System; 
using System.Collections;//需要添加的命名空間 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 哈希表 
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   Hashtable al = new Hashtable(); 
   Console.WriteLine("添加前al的元素個(gè)數(shù)為:"+al.Count); 
   al.Add("1", "a"); 
   al.Add("2", "b"); 
   al.Add("3", "c"); 
   Console.WriteLine("添加后al的元素個(gè)數(shù)為:"+al.Count); 
   Console.ReadLine(); 
  } 
 } 
}

輸出的結(jié)果為:添加前al的元素個(gè)數(shù)為:0
                       添加后al的元素個(gè)數(shù)為:3

二、Hashtable元素的刪除

Hashtable對象的元素的刪除可通過Remove方法,Clear方法來進(jìn)行。

(1).Clear方法將清除所有的元素,其格式為:Hashtable對象.Clear()
(2).Remove方法接受一個(gè)key參數(shù),作用是移除一個(gè)key/value鍵值對,其格式為:Hashtable對象.Remove()

例二、利用上述方法進(jìn)行Hashtable元素的刪除

using System; 
using System.Collections;//需要添加的命名空間 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 哈希表 
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   Hashtable al = new Hashtable(); 
   Console.WriteLine("添加前al的元素個(gè)數(shù)為:"+al.Count); 
   al.Add("1", "a"); 
   al.Add("2", "b"); 
   al.Add("3", "c"); 
   Console.WriteLine("添加后al的元素個(gè)數(shù)為:"+al.Count); 
   al.Remove("3"); 
   Console.WriteLine("刪除3后al的元素個(gè)數(shù)為:"+al.Count); 
   Console.ReadLine(); 
  } 
 } 
}

輸出的結(jié)果為:添加前al的元素個(gè)數(shù)為:0
                       添加后al的元素個(gè)數(shù)為:3
                       刪除C后al的元素個(gè)數(shù)為:2

三、Hashtable元素的遍歷

遍歷哈希表需要用到DictionaryEntry(字典鍵/值對)Object。

例三、利用foreach語句對哈希表進(jìn)行遍歷

using System; 
using System.Collections;//需要添加的命名空間 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 哈希表 
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   Hashtable al = new Hashtable(); 
   Console.WriteLine("添加前al的元素個(gè)數(shù)為:"+al.Count); 
   al.Add("1", "a"); 
   al.Add("2", "b"); 
   al.Add("3", "c"); 
   Console.WriteLine("添加后al的元素個(gè)數(shù)為:"+al.Count); 
   foreach (DictionaryEntry t in al) 
   { 
    Console.Write("鍵位:"+t.Key+" 值為:"); 
    Console.WriteLine(t.Value); 
   } 
   Console.ReadLine(); 
  } 
 } 
}

輸出的結(jié)果為:添加前al的元素個(gè)數(shù)為:0
                       添加后al的元素個(gè)數(shù)為:3
                       鍵位:1 值為:a
                       鍵位:2 值為:b
                       鍵位:3 值為:c

四、Hashtable元素的查找

Hashtable集合提供三個(gè)查找方法查找Hashtable中的元素,這三個(gè)方法為Contains方法,ContainsKe和方法和ContainsValue方法。
Contains方法,ContainsKey方法是根據(jù)Hashtable的key值去查找,如果找到,返回匹配的最后一項(xiàng)的自0開始的索引,否則返回-1,其格式為:
Hashtable對象.Contains(key值)或 Hashtable對象.ContainsKey(key值)
ContainValue方法是根據(jù)Hashtable的value值去查找,如果找到,返回匹配的最后一項(xiàng)自0開始的索引,否則,返回-1,其格式為:Hashtable對象.ContainsValue(Value值) 

例四、利用上述的方法進(jìn)行Hashtable元素的查找

using System; 
using System.Collections;//需要添加的命名空間 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 哈希表 
{ 
 class Program 
 { 
  static void Main(string[] args) 
  { 
   Hashtable al = new Hashtable(); 
   Console.WriteLine("添加前al的元素個(gè)數(shù)為:"+al.Count); 
   al.Add("1", "a"); 
   al.Add("2", "b"); 
   al.Add("3", "c"); 
   Console.WriteLine("添加后al的元素個(gè)數(shù)為:"+al.Count); 
   if (al.Contains("1")) 
   { 
    Console.WriteLine("1存在al中"); 
   } 
   if (al.ContainsKey("2")) 
   { 
    Console.WriteLine("2存在al中"); 
   } 
   if (al.ContainsValue("c")) 
   { 
    Console.WriteLine("c存在al中"); 
   } 
   Console.ReadLine(); 
  } 
 } 
}

輸出的結(jié)果為:添加前al的元素個(gè)數(shù)為:0
                       添加后al的元素個(gè)數(shù)為:3
                       1存在al中
                       2存在al中
                       c存在al中

以上就是關(guān)于C#的哈希表相關(guān)介紹,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論