C # Programming (52)----------ordered list

Source: Internet
Author: User

Ordered list

If you need to sort all the collections based on , you can use the sortedlist<tkey,tvalue> class . This class sorts the elements by key . Any type can be used for values and keys in this collection .

The following example creates an ordered list where the key and value types are string. The default constructor creates an empty list and adds the book with the Add () method . Use the overloaded constructors . You can define the capacity of the list and pass an object that implements the Icomparer<tkey> interface , which is used to sort the elements in the list .

UseADD (Tkey,tvalue)Method,The first parameter is a key, The second parameter is the value . In addition to using add () , You can also use indexers to add elements to the list .< Span style= "font-family: Arial" > indexer needs to use key as index parameter . If the key already exists ,add () method throws a argumentexception. If the resulting use of the same key , Replace the old value with the new value

The sortedlist<tkey,tvalue> class only allows each key to have a corresponding value , and if you need to have more than one key , you can use lookup<tkey,telement > class .

Case :

Using System;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using System.Threading.Tasks;

namespace with sequence list

{

Class Program

{

static void Main (string[] args)

{

If you want to use an ordered table , you can use sortedlist<tkey,tvalue> to sort the elements

sortedlist<string, string> books = new sortedlist<string, string> ();

Books. ADD ("Tangseng", "001");

Books. ADD ("Sunwukong", "002");

Books. ADD ("Zhubajie", "003");

Books. ADD ("Shaheshang", "004");

The key is not allowed to repeat, the following we use the Add method to re- add once tangseng

Books. ADD ("Tangseng", "hahah"); Throw Exception

However, if the index is used to assign a value, if the key exists , then overwrite , not present , is equivalent to using the Add method

books["Tangseng"] = "hahaha";

foreach (var item in books. Keys)

{

Console.WriteLine (item);

}

foreach (var item in books. Values)

{

Console.WriteLine (item);

}

One-time traversal of key values

foreach (keyvaluepair<string, string> item in books)

{

Console.WriteLine (" name : {0}, ordinal : {1}", item. Key, item. Value);

}

/*

* analysis results showed that Tangseng was replaced by hahaha

*

* Below is a brief introduction to the methods and properties in sortedlist<tkey,tvalue>

* Capacity This attribute is used to set the capacity of the given sequence table , as in IList , as well as the exponentially increasing

* Comparer returns the comparator associated with an ordered list , which can be passed in from the constructor

* Remove () RemoveAt () button Delete and delete by index

* ContainsKey (); Containsvalue; Check if there is a key that contains the specified value , or the value

* TryGetValue () try to get the value of the specified key , if there is true, and use out to bring the value back , no is false

*/

Key Delete

Console.WriteLine (" key Delete ");

Books. Remove ("Tangseng");

foreach (keyvaluepair<string, string> item in books)

{

Console.WriteLine (" name : {0}, ordinal : {1}", item. Key, item. Value);

}

you can see that Tangseng was removed.

below we delete by index

Books. RemoveAt (0);

Console.WriteLine (" Delete by index ");

foreach (keyvaluepair<string, string> item in books)

{

Console.WriteLine (" name : {0}, ordinal : {1}", item. Key, item. Value);

}

The results show that Shaheshang is deleted , proving that the deleted index is sorted in order , not in the order of insertion

Check if Tangseng is included , check if Zhubajie is included ( key )

Console.WriteLine (" Check for Tangseng: {0}", books. ContainsKey ("Tangseng"));

Console.WriteLine (" Check for Zhubajie: {0}", books. ContainsKey ("Zhubajie"));

Check if 001 is included , check for 002 ( value )

Console.WriteLine (" contains 001: {0}", books. Containsvalue ("001"));

Console.WriteLine (" contains 002: {0}", books. Containsvalue ("002"));

the index values here are sorted by order

int keyIndex = books. Indexofkey ("Zhubajie");

Console.WriteLine ("Zhubajie index value : {0}", KeyIndex);

String value = "";

if (books. TryGetValue ("Tangseng", out value))

{

Console.WriteLine (" got the value of Tangseng : {0}", value);

}

String value2 = "";

if (books. TryGetValue ("Zhubajie", out value2))

{

Console.WriteLine (" got the value of Zhubajie : {0}", value2);

}

Console.readkey ();

}

}

}

Analysis:UseValuesAndKeysProperty access values and keys.If you try to access an element using an indexer,But the key passed does not exist,It throws an exception..To avoid a, can use containskey () method , If the passed key exists in the collection , This method returns true, can also call method This method attempts to get the value of the specified key . If the specified key corresponds to a value that does not exist , The method throws an exception . /span>

C # Programming (52)----------ordered list

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.