Usage of hashtable in C)

Source: Internet
Author: User
1. the hash table (hashtable) is described in. in the. NET Framework, hashtable is system. A container provided by the collections namespace is used to process and present key-value pairs similar to the keyValue. The key is usually used for quick search, and the key is case sensitive; value is used to store the value corresponding to the key. In hashtable, keyValue pairs are of the object type, so hashtable can support any type of keyValue pairs. 2. Add a key-Value Pair hashtableobject to the hash table for simple operations. add (Key, value); remove a key-Value Pair hashtableobject from the hash table. remove (key); remove all elements from the hash table: hashtableobject. clear (); checks whether the hash table contains the key hashtableobject. contains (key); The following console program will include all the above operations: using system; using system. collections; file when using hashtable, you must introduce this namespace class hashtable {public static void main () {hashtable ht = new hashtable (); file to create a hashtable instance ht. add (E, E); Add the keyValue pair ht. add (a, a); ht. add (C, C); ht. add (B, B); string S = (string) HT [a]; If (HT. contains (e) file determines 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 a ht here. clear (); remove all elements from the console. writeline (HT [a]); file won't have any output here} 3. dictionaryentry object is required to traverse the hash table. The Code is as follows: for (dictionaryentry de in HT) fileht is a hashtable instance {console. writeline (de. key); de. key corresponds to the key-Value Pair key console. writeline (de. value); de. key corresponds to keyValue pair value} 4. Sort the hash table to sort the hash table. The definition here is to rearrange the key in the key-Value Pair according to certain rules, but in fact, this definition cannot be implemented, because we cannot directly rearrange keys in hashtable. If hashtable needs to provide output of certain rules, we can adopt a work und: arraylist akeys = new arraylist (HT. keys); file do not forget to import system. collections akeys. sort (); files are sorted alphabetically for (string skey in akeys) {console. write (skey +); console. writeline (HT [skey]); output after sorting}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.