Csharp/CSharp Tutorial/Data Structure/Hashtable

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add elements to the table and Use the keys to obtain the values

<source lang="csharp">using System; using System.Collections;

class HashtableDemo {

 public static void Main() { 
   Hashtable ht = new Hashtable(); 
    
   
   ht.Add("a", "A"); 
   ht.Add("b", "B"); 
   ht.Add("c", "C"); 
   ht.Add("e", "E"); 

   // Get a collection of the keys. 
   ICollection c = ht.Keys; 

   foreach(string str in c) 
     Console.WriteLine(str + ": " + ht[str]); 
 } 

}</source>

a: A
b: B
c: C
e: E

Add key-value pair to Hashtable by using the indexer

<source lang="csharp">using System; using System.Collections;

class HashtableDemo {

 public static void Main() { 
   Hashtable ht = new Hashtable(); 
    
   
   ht.Add("a", "A"); 
   ht.Add("b", "B"); 
   ht.Add("c", "C"); 
   ht.Add("e", "E"); 
   
   ht["f"] = "F"; 

   // Get a collection of the keys. 
   ICollection c = ht.Keys; 

   foreach(string str in c) 
     Console.WriteLine(str + ": " + ht[str]); 
 } 

}</source>

a: A
b: B
c: C
e: E
f: F

Clear all key/value pairs in a Hashtable

<source lang="csharp">using System; using System.Collections; class MainClass {

 static void Main(string[] args)
 {
   Hashtable a = new Hashtable(10);
         
   a.Add(100, "Arrays");
   a.Add(200, "Classes");
   
   a.Clear();
 }

}</source>

Copy the keys from Hashtable into an array using the CopyTo() method and then display the array contents

<source lang="csharp">using System; using System.Collections; class MainClass {

 public static void Main()
 {
   Hashtable myHashtable = new Hashtable();
   myHashtable.Add("AL", "Alabama");
   myHashtable.Add("CA", "California");
   myHashtable.Add("FL", "Florida");
   myHashtable.Add("NY", "New York");
   myHashtable.Add("WY", "Wyoming");
   foreach (string myKey in myHashtable.Keys)
   {
     Console.WriteLine("myKey = " + myKey);
   }
   foreach(string myValue in myHashtable.Values)
   {
     Console.WriteLine("myValue = " + myValue);
   }
   
   Console.WriteLine("Copying keys to myKeys array");
   string[] myKeys = new string[5];
   myHashtable.Keys.CopyTo(myKeys, 0);
   
   for (int counter = 0; counter < myKeys.Length; counter++) {
     Console.WriteLine("myKeys[" + counter + "] = " + myKeys[counter]);
   }
 }

}</source>

myKey = NY
myKey = CA
myKey = FL
myKey = WY
myKey = AL
myValue = New York
myValue = California
myValue = Florida
myValue = Wyoming
myValue = Alabama
Copying keys to myKeys array
myKeys[0] = NY
myKeys[1] = CA
myKeys[2] = FL
myKeys[3] = WY
myKeys[4] = AL

Copy the values from Hashtable into an array using the CopyTo() method and then display the array contents

<source lang="csharp">using System; using System.Collections; class MainClass {

 public static void Main()
 {
   Hashtable myHashtable = new Hashtable();
   myHashtable.Add("AL", "Alabama");
   myHashtable.Add("CA", "California");
   myHashtable.Add("FL", "Florida");
   myHashtable.Add("NY", "New York");
   myHashtable.Add("WY", "Wyoming");
   foreach (string myKey in myHashtable.Keys)
   {
     Console.WriteLine("myKey = " + myKey);
   }
   foreach(string myValue in myHashtable.Values)
   {
     Console.WriteLine("myValue = " + myValue);
   }
   
   Console.WriteLine("Copying values to myValues array");
   string[] myValues = new string[5];
   myHashtable.Values.CopyTo(myValues, 0);
   for (int counter = 0; counter < myValues.Length; counter++)
   {
     Console.WriteLine("myValues[" + counter + "] = " + myValues[counter]);
   }
 }

}</source>

myKey = NY
myKey = CA
myKey = FL
myKey = WY
myKey = AL
myValue = New York
myValue = California
myValue = Florida
myValue = Wyoming
myValue = Alabama
Copying values to myValues array
myValues[0] = New York
myValues[1] = California
myValues[2] = Florida
myValues[3] = Wyoming
myValues[4] = Alabama

Mark a Hashtable to be Synchronized

<source lang="csharp">using System; using System.Collections; class MainClass {

 static void Main(string[] args)
 {
   Hashtable a = new Hashtable(10);
   Hashtable.Synchronized(a);
         
 }

}</source>

Remove key/value pairs from Hashtable

<source lang="csharp">using System; using System.Collections; class MainClass {

 static void Main(string[] args)
 {
   Hashtable a = new Hashtable(10);
         
   a.Add(100, "A");
   a.Add(200, "C");
   
   a.Remove(100);
   a.Remove(200);
 }

}</source>

Use foreach statement to loop through all keys in a hashtable

<source lang="csharp">using System; using System.Collections; class MainClass {

   public static void Main()
   {
       Hashtable    hash = new Hashtable();
       hash.Add("A", "1");
       hash.Add("B", "2");
       hash.Add("C", "3");
       hash.Add("D", "4");
       hash.Add("E", "5");
       
       foreach (string firstName in hash.Keys)
       {
           Console.WriteLine("{0} {1}", firstName, hash[firstName]);
       }
   }

}</source>

A 1
B 2
C 3
D 4
E 5

Use the ContainsKey() method to check if Hashtable contains a key

<source lang="csharp">using System; using System.Collections; class MainClass {

 public static void Main()
 {
   Hashtable myHashtable = new Hashtable();
   myHashtable.Add("AL", "Alabama");
   myHashtable.Add("CA", "California");
   myHashtable.Add("FL", "Florida");
   myHashtable.Add("NY", "New York");
   myHashtable.Add("WY", "Wyoming");
   foreach (string myKey in myHashtable.Keys)
   {
     Console.WriteLine("myKey = " + myKey);
   }
   foreach(string myValue in myHashtable.Values)
   {
     Console.WriteLine("myValue = " + myValue);
   }
   
   if (myHashtable.ContainsKey("FL"))
   {
     Console.WriteLine("myHashtable contains the key FL");
   }
 }

}</source>

myKey = NY
myKey = CA
myKey = FL
myKey = WY
myKey = AL
myValue = New York
myValue = California
myValue = Florida
myValue = Wyoming
myValue = Alabama
myHashtable contains the key FL

Use the ContainsValue() method to check if Hashtable contains a value

<source lang="csharp">using System; using System.Collections; class MainClass {

 public static void Main()
 {
   Hashtable myHashtable = new Hashtable();
   myHashtable.Add("AL", "Alabama");
   myHashtable.Add("CA", "California");
   myHashtable.Add("FL", "Florida");
   myHashtable.Add("NY", "New York");
   myHashtable.Add("WY", "Wyoming");
   foreach (string myKey in myHashtable.Keys)
   {
     Console.WriteLine("myKey = " + myKey);
   }
   foreach(string myValue in myHashtable.Values)
   {
     Console.WriteLine("myValue = " + myValue);
   }
   
   if (myHashtable.ContainsValue("Florida"))
   {
     Console.WriteLine("myHashtable contains the value Florida");
   }
 }

}</source>

myKey = NY
myKey = CA
myKey = FL
myKey = WY
myKey = AL
myValue = New York
myValue = California
myValue = Florida
myValue = Wyoming
myValue = Alabama
myHashtable contains the value Florida

Use the Remove() method to remove FL from Hashtable

<source lang="csharp">using System; using System.Collections; class MainClass {

 public static void Main()
 {
   Hashtable myHashtable = new Hashtable();
   myHashtable.Add("AL", "Alabama");
   myHashtable.Add("CA", "California");
   myHashtable.Add("FL", "Florida");
   myHashtable.Add("NY", "New York");
   myHashtable.Add("WY", "Wyoming");
   foreach (string myKey in myHashtable.Keys)
   {
     Console.WriteLine("myKey = " + myKey);
   }
   foreach(string myValue in myHashtable.Values)
   {
     Console.WriteLine("myValue = " + myValue);
   }
   
   Console.WriteLine("Removing FL from myHashtable");
   myHashtable.Remove("FL");
 }

}</source>

myKey = NY
myKey = CA
myKey = FL
myKey = WY
myKey = AL
myValue = New York
myValue = California
myValue = Florida
myValue = Wyoming
myValue = Alabama
Removing FL from myHashtable