1. Brief description of the hash table (Hashtable)
In. NET Framework, Hashtable is a capacity provided by the System. Collections namespace.
Is used to process and display key-value pairs similar to keyvalue. The key is usually used for quick search.
Key is case sensitive; value is used to store the value corresponding to the key. All keyvalue pairs in Hashtable
It is of the object type, so Hashtable can support any type of keyvalue key-value pairs.
Ii. Simple operations on Hash Tables
Add a keyvalue key-value pair in the hash table: HashtableObject. Add (key, value );
Remove a keyvalue key-value pair in the hash table: HashtableObject. Remove (key );
Remove all elements from the hash table: HashtableObject. Clear ();
Determine whether the hash table Contains the specified key: HashtableObject. Contains (key );
The following console contains all the preceding operations:
[Csharp]
Using System;
Using System. Collections; // file this namespace must be introduced when Hashtable is used.
Class hashtable
{
Public static void Main ()
{
Hashtable ht = new Hashtable (); // file to create a Hashtable instance
Ht. Add (E, e); // Add a keyvalue pair
Ht. Add (A, );
Ht. Add (C, c );
Ht. Add (B, B );
String s = (string) ht [A];
If (ht. Contains (E) // file to determine whether a hash table Contains a specific key. The return value is true or false.
Console. WriteLine (the E keyexist );
Ht. Remove (C); // Remove a keyvalue pair
Console. WriteLine (ht [A]); // output
Ht. Clear (); // remove all elements
Console. WriteLine (ht [A]); // file there will be no output here
}
}
Using System;
Using System. Collections; // file this namespace must be introduced when Hashtable is used.
Class hashtable
{
Public static void Main ()
{
Hashtable ht = new Hashtable (); // file to create a Hashtable instance
Ht. Add (E, e); // Add a keyvalue pair
Ht. Add (A, );
Ht. Add (C, c );
Ht. Add (B, B );
String s = (string) ht [A];
If (ht. Contains (E) // file to determine whether a hash table Contains a specific key. The return value is true or false.
Console. WriteLine (the E keyexist );
Ht. Remove (C); // Remove a keyvalue pair
Console. WriteLine (ht [A]); // output
Ht. Clear (); // remove all elements
Console. WriteLine (ht [A]); // file there will be no output here
}
}
3. traverse the hash table
DictionaryEntry Object is required to traverse a hash table. The Code is as follows:
[Csharp]
For (DictionaryEntry de in ht) fileht is a Hashtable instance.
{
Console. WriteLine (de. Key); // de. Key corresponds to the key
Console. WriteLine (de. Value); // de. Key corresponds to the Key-value Pair
}
For (DictionaryEntry de in ht) fileht is a Hashtable instance.
{
Console. WriteLine (de. Key); // de. Key corresponds to the key
Console. WriteLine (de. Value); // de. Key corresponds to the Key-value Pair
}
4. Sort hash tables
Sort the hash table. The definition here is to re-arrange the keys in the key-Value Pair according to certain rules,
Actually, this definition cannot be implemented, because we cannot directly redo the key in Hashtable.
Arrangement. If Hashtable is required to provide the output of certain rules, a work und can be adopted:
[Csharp]
ArrayList akeys = new ArrayList (ht. Keys); // file do not forget to import System. Collections
Akeys. Sort (); // Sort files alphabetically.
For (string skey in akeys)
{
Console. Write (skey + );
Console. WriteLine (ht [skey]); output after sorting
}
ArrayList akeys = new ArrayList (ht. Keys); // file do not forget to import System. Collections
Akeys. Sort (); // Sort files alphabetically.
For (string skey in akeys)
{
Console. Write (skey + );
Console. WriteLine (ht [skey]); output after sorting
}