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. The instance 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.
Assuming that comparer is a null reference (Nothing in Visual Basic), the IComparable implementation of each key is used. Therefore, each key must implement the IComparable interface so that it can be compared with every other key in the SortedList.
The capacity of the SortedList is the number of elements that SortedList can hold. When adding an element to SortedList, the internal array is allocated once again, depending on the need to proactively increase the capacity.
Assume the ability to anticipate the size of the collection. When the initial capacity is specified, there is no need to run a large number of sizing 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 includes elements copied from the specified dictionary, with the same initial capacity as the number of elements copied, and sorted by the IComparable interface implemented by each key.
Each key must implement the IComparable interface. So that it can be compared with every other key in the SortedList.
Sorts the elements according to the IComparable implementation of each key added to SortedList.
Hashtable is a demonstration example of a IDictionary implementation that can be passed to this constructor. The new SortedList includes 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 adding an element to SortedList, the internal array is allocated once again, depending on the need to proactively increase the capacity.
assume the ability to anticipate the size of the collection. When the initial capacity is specified, there is no need to run a large number of sizing 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 (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. The instance includes elements copied from the specified dictionary, with the same initial capacity as the number of elements copied, and sorted by 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 visited by its keys (such as arbitrary IDictionary elements in the implementation). or by its index, such as an element in an IList implementation.
SortedList maintains two arrays internally to store the elements in the list, that is, an array for the key, and an array for the associated values.
Each element is a key/value pair that can be visited as a DictionaryEntry object. The key cannot be a null reference (Nothing in Visual Basic). But the value can.
The capacity of the SortedList is the number of elements that SortedList can hold.
The default initial capacity of SortedList is 0.
As the element is added to the SortedList, it is possible to add capacity again by assigning itself once and for all as needed.
You can reduce the capacity by calling TrimToSize or by explicitly setting the capacity property.
The elements of the 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 the key.
In either case, the SortedList is different from the repeated key.
The index order is based on the sort order.
When an element is added, the element inserts SortedList in the correct sort order. At the same time, the index is adjusted accordingly. When the element is removed. The index is also adjusted accordingly. Therefore, when elements are added to or removed from SortedList. The index of a particular key/value pair may change.
because you want to sort. Therefore, the operation on the SortedList is slower than the operation on the Hashtable. But. SortedList agrees to provide greater flexibility by providing access to the values through the associated keys or through an index. You can use an integer index to access the elements in this collection.
The indexes in this collection start 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 the 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.
/span>
Use of SortedList classes in C #