Summary of various sets in C #

Source: Internet
Author: User
1. Let's talk about the shortcomings of arrays (or the difference between a set and an array): 1. arrays are fixed in size and cannot be scaled. Although the generic method system. array. Resize can reset the array size, this method re-creates an array with the new size and initializes it with elements of the old array. Then the previous array will be discarded! But the set is variable length 2. The array should declare the element type, the element type of the Collection class is object.3. the array is readable and writable, And the read-only array cannot be declared. The Collection class can provide the readonly method to use the set in read-only mode. 4. the array must have an integer subscript to access specific elements. However, such subscript is not very useful in many cases. II. the following describes six common collections. arraylist class using system; using system. collections. generic; using system. text; using system. collections; namespace consoleapplication1... {class program... {static void main (string [] ARGs )... {arraylist Al = new arraylist (); Al. add (100); // Add foreach (INT number in New int [6]... {9, 3, 7, 2, 4, 8 })... {Al. add (number); // Add group method 1 // clear moon http://blog.csdn.net/21aspnet/} int [] number2 = new Int [2]... {11,12}; Al. addrange (number2); // Add method 2. remove (3); // remove Al with a value of 3. removeat (3); // remove 3rd arraylist Al = new arraylist (Al. getrange (1, 3); // The New arraylist takes only a portion of the old arraylist as the console. writeline ("Traversal method 1:"); foreach (int I in Al) // do not forcibly convert... {console. writeline (I); // Traversal method 1} console. writeline ("Traversal method 2:"); For (INT I = 0; I! = Al2.count; I ++) // The array is length... {int number = (INT) al [I]; // The console must be forcibly converted. writeline (number); // Traversal method 2 }}} 2. stack class Stack, followed by first-in-first-out. Push method into the stack, pop method out of the stack. Using system; using system. collections. generic; using system. text; using system. collections; namespace consoleapplication1... {class program... {static void main (string [] ARGs )... {stack Sk = new stack (); stack sk2 = new stack (); foreach (int I in new int [4]... {1, 2, 3, 4 })... {SK. push (I); // fill sk2.push (I);} foreach (int I in SK )... {console. writeline (I); // traversal} SK. pop (); console. writeline ("pop "); Foreach (int I in SK )... {console. writeline (I);} sk2.peek (); // The last item does not delete // clear month http://blog.csdn.net/21aspnet/ console. writeline ("peek"); foreach (int I in sk2 )... {console. writeline (I);} while (sk2.count! = 0 )... {int I = (INT) sk2.pop (); // clear sk2.pop (); // clear} console. writeline ("clear"); foreach (int I in sk2 )... {console. writeline (I) ;}}} 3. queue class, first-in-first-out. The enqueue method is used to enter the queue, and the dequeue method is used to output the queue. Using system; using system. collections. generic; using system. text; using system. collections; namespace consoleapplication1... {class program... {static void main (string [] ARGs )... {queue Qu = new Queue (); queue qu2 = new Queue (); foreach (int I in new int [4]... {1, 2, 3, 4 })... {Qu. enqueue (I); // fill in qu2.enqueue (I);} foreach (int I in Qu )... {console. writeline (I); // traversal} Qu. dequeue (); console. writel INE ("dequeue"); foreach (int I in Qu )... {console. writeline (I);} qu2.peek (); // The last item does not delete the console. writeline ("peek"); foreach (int I in qu2 )... {console. writeline (I);} while (qu2.count! = 0 )... {int I = (INT) qu2.dequeue (); // clear qu2.dequeue (); // clear} console. writeline ("clear"); foreach (int I in qu2 )... {console. writeline (I) ;}}} 4. hashtable hash table, name-value pair. Similar to a dictionary (more powerful than an array ). The hash table is optimized, and the objects accessing the underlying object are first hashed. If any type of key value is used to access the element, it is faster than other sets. The gethashcode () method returns an int type data. The value of this key is used to generate the int type data. The hash table returns an index after obtaining this value, indicating the location where the data items with the given hash are stored in the dictionary. Using system; using system. collections. generic; using system. text; using system. collections; namespace consoleapplication1... {class program... {public static void main ()... {// creates and initializes a new hashtable. hashtable myht = new hashtable (); myht. add ("one", "the"); myht. add ("two", "quick"); myht. add ("three", "brown"); myht. add ("four", "Fox"); // displays the hashtable. // clear moon http://blog.cs Dn.net/21aspnet/ console. writeline ("The hashtable contains the following:"); printkeysandvalues (myht);} public static void printkeysandvalues (hashtable myht )... {foreach (string s in myht. keys) console. writeline (s); console. writeline ("-key--value-"); foreach (dictionaryentry de in myht) console. writeline ("{0 }:{ 1}", de. key, de. value); console. writeline () ;}} 5. the sortedlist class is similar to the hash table. The difference is that sortedlis The key array in T is sorted in order. Using system; using system. collections. generic; using system. text; using system. collections; namespace consoleapplication1... {class program... {public static void main ()... {sortedlist SL = new sortedlist (); SL ["C"] = 41; SL ["A"] = 42; SL ["D"] = 11; SL ["B"] = 13; foreach (dictionaryentry element in SL )... {string S = (string) element. key; int I = (INT) element. value; console. writeline ("{0}, {1}", S, I );} 6. namevaluecollection officially defines namevaluecollection as a special collection class under system. Collections. Specialized. The hybriddicionary class is also available under system. Collections. Specialized. We recommend that you use hybriddicionary for less than 10 elements. When the element is increased, it is automatically converted to hashtable. Under system. Collections. Specialized, there is also the hybriddicionary class, a string set. There are other classes available under system. Collections. Specialized! The main explanation is namevaluecollection. hashtable and namevaluecollection are similar, but they are different. The key of hashtable is unique, and namevaluecollection is not unique! Using system; using system. collections. generic; using system. collections; using system. collections. specialized; namespace consoleapplication1... {class program... {static void main (string [] ARGs )... {system. collections. hashtable ht = new system. collections. hashtable (); ht. add ("ddpmdisplayseq ". trim (), "display sequence ". trim (); ht. add ("ddpmnamechi ". trim (), "Name (Chinese )". trim (); ht. add ("ddpmnameeng ". trim (), "Name (English )". trim (); ht. add ("comment ". trim (), "comment ". trim (); ht. add ("ddpmmarketcode ". trim (), "market code ". trim (); foreach (Object key in HT. keys )... {console. writeline ("{0}/{1} {2}, {3}", key, HT [Key], key. gethashcode (), HT [Key]. gethashcode ();} console. writeline (""); // clear moon http://blog.csdn.net/21aspnet/ namevaluecollection mycol = new namevaluecollection (); mycol. add ("ddpmdisplayseq ". trim (), "display sequence ". trim (); mycol. add ("ddpmnamechi ". trim (), "Name (Chinese )". trim (); mycol. add ("ddpmnamechi ". trim (), "Name (English )". trim (); mycol. add ("comment ". trim (), "comment ". trim (); mycol. add ("ddpmmarketcode ". trim (), "market code ". trim (); foreach (string key in mycol. keys )... {console. writeline ("{0}/{1} {2}, {3}", key, mycol [Key], key. gethashcode (), mycol [Key]. gethashcode ());}}}}
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.