C # common collection classes

Source: Internet
Author: User

C # common collection classes

Collection)

ArrayList:

Using System; using System. collections. generic; using System. text; using System. collections; namespace SpaceName {class Program {static void Main (string [] args) {ArrayList array = newArrayList (); array. add (100); // Add an object {100} foreach (int number in new int [6] {9, 3, 7, 2, 4, 8}) {array. add (number); // Add multiple objects array = {100, 9, 3, 7, 2, 4, 8} int [] number2 = new int [2] {11,12}; array. addRange (nu Mber2); // Add an array object array = {100, 9, 3, 7, 2, 4, 12} array. remove (3); // Remove array = {100, 9, 4, 12} array with a value of 3. removeAt (3); // remove 3rd arrays = {100, 9, 12} ArrayList array2 = new ArrayList (array. getRange (1, 3); // The New ArrayList takes only a portion of the old ArrayList. array2 = {9, 7, 4,} Console. writeLine ("Traversal method 1:"); foreach (int I in array) // do not forcibly convert {Console. writeLine (I); // Traversal method 1} Console. writeLine ("Traversal method 2:"); for (in T I = 0; I! = Array2.Count; I ++) // The array is length {int number = (int) array2 [I]; // The Console must be forcibly converted. writeLine (number); // Traversal method 2 }}}}

Stack class andQueueClass:

System.Collections.Stack stack=new System.Collections.Stack();stack.Push(1);stack.Push(2);stack.Push(3);stack.Push(4);stack.Push(5);while(stack.Count>0){System.Console.WriteLine(stack.Pop());}

System.Collections.Queue queue=new System.Collections.Queue();queue.Enqueue(1);queue.Enqueue(2);queue.Enqueue(3);queue.Enqueue(4);queue.Enqueue(5);while(queue.Count>0){System.Console.WriteLine(queue.Dequeue());}

 Dictionary

Dictionary <string, string> myDic = new Dictionary <string, string> (); myDic. add ("aaa", "111"); myDic. add ("bbb", "222"); myDic. add ("ccc", "333"); myDic. add ("ddd", "444"); // if an existing key is added, the add method throws an exception. try {myDic. add ("ddd", "ddd");} catch (ArgumentException ex) {Console. writeLine ("this key already exists:" + ex. message);} // The ContainsKey () method is used to determine whether the key exists if (! MyDic. containsKey ("ddd") {myDic. add ("ddd", "ddd");} else {Console. writeLine ("this key already exists:");} // when the index is used for negative values, if the key already exists, the key value of the existing key is modified, and does not throw an exception. myDic ["ddd"] = "ddd"; myDic ["eee"] = "555"; // when the indexer is used for value, if the key does not exist, an exception will be thrown. try {Console. writeLine ("nonexistent key" "fff" "has the key value:" + myDic ["fff"]);} catch (KeyNotFoundException ex) {Console. writeLine ("the key is not found and an exception is thrown:" + ex. message) ;}// you can use ContarnsKey () to determine whether a key exists, if you often need to calculate the key value, it is best to use the TryGetValue method to obtain the corresponding key value in the Set string value = ""; if (myDic. tryGetValue ("fff", out value) {Console. writeLine ("nonexistent key" "fff" "key value:" + value);} else {Console. writeLine ("the corresponding key value is not found");} // The following uses foreach to traverse the key-Value Pair // generic struct to store the key-Value Pair foreach (KeyValuePair <string, string> kvp in myDic) {Console. writeLine ("key = {0}, value = {1}", kvp. key, kvp. value);} // obtain the Value set foreach (string s in myDic. values) {Console. writeLine ("value = {0}", s) ;}// obtain a Dictionary <string, string>. valueCollection values = myDic. values; foreach (string s in values) {Console. writeLine ("value = {0}", s );}

HashTable:

 List

List <string> names = new List <string> (); names. add ("Qin Yu"); names. add ("ye Chen"); names. add ("ye fan"); // traverse Listforeach (string name in names) {Console. writeLine (name);} // Insert the element names to the List. insert (2, "Lin Dong"); // remove the specified element names. remove ("ye fan ");
Although the knowledge is relatively basic, it still gives a lot of inspiration to myself. learning is a process of constant contact, and then gradually deepen understanding, instead of being a fat man.

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.