Use of the SortedList class in C #

Source: Internet
Author: User

SortedList classes in C #

namespaces: System.Collections

Assembly: mscorlib (in mscorlib.dll)

syntax: public class sortedlist:idictionary, ICollection, IEnumerable, ICloneable


constructor Function:

1. SortedList ()

Initializes a new instance of the SortedList class that is empty, has a default initial capacity, and is sorted according to the IComparable interface (implemented by each key added to the SortedList)

Example:

SortedList mySL1 = new SortedList ();

2. SortedList (IComparer)

Initializes a new instance of the SortedList class that is empty, has a default initial capacity, and is sorted according to the specified IComparer interface.

sorts the elements according to the specified IComparer implementation. If comparer is a null reference (Nothing in Visual Basic), the IComparable implementation of each key is used, so each key must implement the IComparable interface so that it can be compared to each of the other keys in the SortedList More.

the capacity of the SortedList is the number of elements that SortedList can hold. When you add an element to SortedList, the capacity is automatically increased as needed by reallocating the internal array.

If you can estimate the size of the collection, when you specify the initial capacity, you do not have to perform a large number of resizing operations when adding elements to SortedList.

The operation complexity of this constructor is O (1).

Example:

SortedList mySL2 = new SortedList (new CaseInsensitiveComparer ());

3. SortedList (IDictionary)

Initializes a new instance of the SortedList class that contains elements copied from the specified dictionary, has the same initial capacity as the number of elements copied, and is sorted according to the IComparable interface implemented by each key.

Each key must implement the IComparable interface so that it can be compared to each of the other keys in the SortedList. Sorts the elements according to the IComparable implementation of each key added to SortedList.

Hashtable is an example of a IDictionary implementation that can be passed to this constructor. The new SortedList contains a copy of the keys and values stored in the Hashtable.

the capacity of the SortedList is the number of elements that SortedList can hold. When you add an element to SortedList, the capacity is automatically increased as needed by reallocating the internal array.

If you can estimate the size of the collection, when you specify the initial capacity, you do not have to perform a large number of resizing operations when adding elements to SortedList.

The operation complexity of this constructor is O (n), where n is the number of elements in D.

Example:

Hashtable myht = new Hashtable ();

Myht.add ("First", "Hello");

Myht.add ("SECOND", "World");

Myht.add ("Third", "!");

//Create a SortedList using the default comparer.

SortedList mySL3 = new SortedList (MYHT);

4. SortedList (Int32)

Initializes a new instance of the SortedList class that is empty, has the specified initial capacity, and is sorted according to the IComparable interface, which is implemented by each key added to the SortedList.

Example:

SortedList mySL4 = new SortedList (3);

Console.WriteLine ("MySL1 (default):");

Mysl4.add ("First", "Hello");

Mysl4.add ("SECOND", "World");

Mysl4.add ("Third", "!");

5. SortedList (IComparer, Int32)

Initializes a new instance of the SortedList class that is empty, has the specified initial capacity, and is sorted according to the specified IComparer interface.

Example:

SortedList mySL5 = new SortedList (new CaseInsensitiveComparer (), 3);

Console.WriteLine ("MySL2 (case-insensitive comparer):");

Mysl5.add ("First", "Hello");

Mysl5.add ("SECOND", "World");

Mysl5.add ("Third", "!");

6. SortedList (IDictionary, IComparer)

Initializes a new instance of the SortedList class that contains elements copied from the specified dictionary, has the same initial capacity as the number of elements copied, and is sorted according to the specified IComparer interface.

Example:

Hashtable myht = new Hashtable ();

Myht.add ("First", "Hello");

Myht.add ("SECOND", "World");

Myht.add ("Third", "!");

SortedList mySL6 = new SortedList (MYHT, New CaseInsensitiveComparer ());

Method:

1. Adding elements

SortedList MYSL = new SortedList ();

Mysl.add ("One", "the");

Mysl.add ("Both", "quick");

Mysl.add ("Three", "Brown");

Mysl.add ("Four", "Fox");

at this point, Mysl.count = 4, mysl.capacity =

2. Clear elements

mysl.clear ();

at this time, Mysl.count = 0, mysl.capacity = +;

3. Whether to include a specific key (ContainsKey), a specific value (Containsvalue)

SortedList MYSL = new SortedList ();

Mysl.add (2, "a");

Mysl.add (4, "four");

Mysl.add (1, "one");

Mysl.add (3, "three");

mysl.add (0, "zero");

int myKey = 2;

bool F1 = Mysl.containskey (MyKey);

String myvalue = "three";

bool F2 = Mysl.containsvalue (myvalue);

F1 is ture,f2 true at this time

4. Get Key (Getbyindex), Value (GetKey)

SortedList MYSL = new SortedList ();

Mysl.add (1.3, "fox");

Mysl.add (1.4, "jumped");

Mysl.add (1.5, "over");

Mysl.add (1.2, "Brown");

Mysl.add (1.1, "quick");

Mysl.add (1.0, "the");

Mysl.add (1.6, "the");

Mysl.add (1.8, "dog");

Mysl.add (1.7, "lazy");

int myindex=3;

Console.WriteLine ("The key at index {0} is {1}.", Myindex, Mysl.getkey (Myindex));

Console.WriteLine ("The value at index {0} is {1}.", Myindex, Mysl.getbyindex (Myindex));

The key at index 3 is 1.3. The value at index 3 is Fox.

*************************************************************************************************

Note

The SortedList element can be accessed by its key (such as an element in any IDictionary implementation), or by its index (such as an element in any IList implementation).

SortedList maintains two arrays internally to store the elements in the list, that is, one array is used for the key and the other array is used for the associated value.

Each element is a key/value pair that can be accessed as a DictionaryEntry object. The key cannot be a null reference (Nothing in Visual Basic), but the value is OK.

the capacity of the SortedList is the number of elements that SortedList can hold. The default initial capacity of SortedList is 0. As elements are added to the SortedList, the capacity can be automatically increased by reallocation when needed. Capacity can be reduced by calling trimtosize or by explicitly setting the capacity property.

The elements of SortedList will be implemented according to the specific IComparer implementation (as specified when creating the SortedList) or by the IComparable implementation provided by the key itself and sorted by key.

in either case, the SortedList does not allow duplicate keys.

The index order is based on the sort order. When elements are added, the elements are inserted into the SortedList in the correct sort order, and the indexes are adjusted accordingly. When an element is removed, the index is adjusted accordingly. Therefore, when you add or remove elements in SortedList, the index of a particular key/value pair may change.

because of sorting, it is slower to operate on SortedList than on Hashtable. However, SortedList allows for greater flexibility by allowing access to values through associated keys or through an index. You can use an integer index to access the elements in this collection. The index in this collection starts from zero.

The foreach statement in the C # language (for each in Visual Basic) requires the type of each element in the collection. Because each element of SortedList is a key/value pair, the element type is neither the type of the key nor the type of the value. But the DictionaryEntry type.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use of the SortedList class in C #

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.