C # common collections

Source: Internet
Author: User

I. Let's talk about it first.ArrayOfInsufficient(You can also saySetDifferences from arrays ):

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! The set is variable-length.

2. the array must declare the element type. The element type of the Collection class is object.

3. the read-only array cannot be declared for readable and writable arrays. 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. The set is also a data list, but it does not use subscript access. Most of the time there is a set of custom subscript types, which does not support subscript access for queues and stacks!

 

2. Six common collections are described below

1. 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 Child http://blog.csdn.net/21aspnet/?int=] number2 = new int [2]... {11,12}; Al. addrange (numbe R2); // Add group method 2 Al. 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

Stack,Back-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 in sk2.push (I);} foreach (int I in SK) {console. writeline (I); // traversal} SK. pop (); console. writeline ("pop"); foreach (int I in SK) {console. W Riteline (I);} sk2.peek (); // The last item does not delete // clear moon 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

Queue,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. writeline ("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 class

Hash table,Name-ValueYes. 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.csdn.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. sortedlist class

Similar to a hash table, the difference is that the key array in sortedlist 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 class

Namevaluecollection is officially defined 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 ());}}}}

Link: http://blog.csdn.net/21aspnet/article/details/1667862

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.