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 . The values and keys in this collection can use any type .
The following example creates an ordered list where the key and value types are string. The default constructor creates an empty list , re-use Add () method Add a book . using overloaded constructors . you can define the capacity of a list , Delivery implements the icomparer<tkey> object of the interface , This interface is used to sort the elements in the list .
UseAdd (Tkey,tvalue)Method,The first parameter is a key,The second parameter is a value.in addition to usingAdd ()outside of the method,You can also use the indexer to add elements to the list.indexers need to use keys as index parameters.if the key already exists, ADD ()method throws aArgumentExceptiontype of Exception.if the use of the same key is caused,replace the old value with the new value.
The sortedlist<tkey,tvalue> class only allows each key to have a corresponding value , and if each key is required to correspond to more than one , You can use the 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 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 value
* TryGetValue () attempts to get the value of the specified key , If any, is true and takes the value back with out , no , it's 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:UseValuesand theKeysproperty 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 useContainsKey ()Method,if the passed key exists in the collection,This method returnstrue,You can also callTryGetValue ()Method,the method attempts to get the value of the specified key.if the value corresponding to the specified key does not exist,the method throws an exception.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C # Advanced Programming 52-day----ordered list