Represents a collection of key/value pairs that are sorted by keys and are accessed by key and index.
SortedList is best suited to sort a set of health/value pairs, and when sorted, the keys are sorted, SortedList is a mixture of Hashtable and array. When you use the Item indexer property to access an element by its key, its behavior is similar to Hashtable. When you use Getbyindex or setbyindex to access an element by its index, it behaves like an Array.
SortedList maintains two arrays internally to store the array 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 the list can have. As you add elements to the SortedList, the capacity is automatically incremented on demand by reallocating. 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. If you remove an element, 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.
One. Add Delete
1. public virtual void Add (object key,object value);
The index in this collection starts from zero.
Adds an element with the specified key and value to the SortedList.
The Item property can also be used to add a new element by setting the value of a key that does not exist in SortedList. For example: mycollection["myNonexistentKey"] = myvalue. However, if the specified key already exists in SortedList, setting the Item property overwrites the old value. In contrast, the Add method does not modify existing elements.
Copy the Code code as follows:
SortedList sList = new SortedList ();
Slist.add (1, "D");
Slist.add (2, "C");
Slist.add (3, "B");
Slist.add (4, "a");
The result is D C b A, so it's a key sort, not a value sort.
Dropdownlist3.datasource = sList;
Dropdownlist3.datatextfield = "Key";
Dropdownlist3.datavaluefield = "Value";
Dropdownlist3.databind ();
2. public virtual void Remove (object key);
Removes the element with the specified key from the SortedList
If the SortedList does not contain an element with the specified key, the SortedList remains unchanged. Do not throw an exception
Copy the Code code as follows:
SortedList sList = new SortedList ();
Slist.add (1, "D");
Slist.add (2, "C");
Slist.add (3, "B");
Slist.add (4, "a");
Slist.remove ("B"); Error, is deleted by key, not value
Slist.remove (3); deleted [3, "B"]
Dropdownlist3.datasource = sList;
Dropdownlist3.datatextfield = "Key";
Dropdownlist3.datavaluefield = "Value";
Dropdownlist3.databind ();
3. public virtual void RemoveAt (int index);
Removes the element at the specified index of the SortedList.
Copy the Code code as follows:
SortedList sList = new SortedList ();
Slist.add (1, "D");
Slist.add (2, "C");
Slist.add (3, "B");
Slist.add (4, "a");
Slist.removeat (3); The deletion is [4, "a"], where the parameter is the index number, not the key value,
Different from Slist.remove (3); Slist.remove (3) deleted [3, "B"]
Dropdownlist3.datasource = sList;
Dropdownlist3.datatextfield = "Key";
Dropdownlist3.datavaluefield = "Value";
Dropdownlist3.databind ();
4. public virtual void Clear ();
Remove all elements from SortedList count is set to zero. The capacity remains unchanged. To reset the capacity of the SortedList, call trimtosize or set the capacity property directly. Truncating the empty SortedList will set the SortedList capacity to the default capacity instead of 0
Two. Index-related operations
1. public virtual void Setbyindex (intindex,object value);
Replaces the value at the specified index in the SortedList.
Copy the Code code as follows:
SortedList sList = new SortedList ();
Slist.add (1, "D");
Slist.add (2, "C");
Slist.add (3, "B");
Slist.add (4, "a");
Slist.setbyindex (1, "dddddd"); 1 is an index, if count<2, an error, that is, must exist
and slist[2] = "DDDDDD"; there is no such phenomenon,
meaning slist[2] = "DDDDDD" is
If the key exists in the modified value, does not exist then add
Dropdownlist3.datasource = sList;
Dropdownlist3.datatextfield = "Key";
Dropdownlist3.datavaluefield = "Value";
Dropdownlist3.databind ();
2. Public virtual Object Getbyindex (Intindex);
Gets the value at the specified index of the SortedList. Index must be less than count, otherwise an error
Copy the Code code as follows:
SortedList sList = new SortedList ();
Slist.add (1, "D");
Slist.add (2, "C");
Slist.add (3, "B");
Slist.add (4, "a");
Slist.clear ();
int nIndex = 2;
if (Nindex<slist.count)
{
Label3.text = Slist.getbyindex (NIndex). ToString ();
}
Else
{
Label3.text = "Nindex>=count";
}
3.public virtual int Indexofkey (Objectkey);
Returns the index of the specified key from the SortedList, which is not Hashtable, because Hashtable does not have the concept of order, its sort is internal
4.public virtual int indexofvalue (objectvalue);
Returns the index of the first occurrence of the specified value in SortedList, which is not Hashtable, because Hashtable does not have an orderly concept, and its ordering is internal
Copy the Code code as follows:
SortedList sList = new SortedList ();
Slist.add (1, "D");
Slist.add (2, "C");
Slist.add (3, "B");
Slist.add (4, "a");
Slist.add (5, "D");
int nIndex = 0;
NIndex = Slist.indexofkey (1); is 0
NIndex = Slist.indexofvalue ("D"); The value matches two, then the first match is returned, so 0
Three. Other
1.public Virtual Object GetKey (int index);
Gets the key at the specified index of the SortedList, which is also hashtable impossible
2.public virtual IList getkeylist ();
Get the keys in SortedList
Copy the Code code as follows:
SortedList sList = new SortedList ();
Slist.add (1, "D");
Slist.add (2, "C");
Slist.add (3, "B");
Slist.add (4, "a");
Slist.add (5, "D");
Label3.text = "";
IList IList = Slist.getkeylist ();
for (int i=0; i<slist.count; i++)
{
Label3.text + = Ilist[i]. ToString ();
Label3.text + = "";
}
Note: The IList interface, which represents a set of objects that can be accessed individually by index, has an item property, which in C # is the indexer
3.public virtual IList getvaluelist ();
Get the value in SortedList
4.public virtual bool Contains (Objectkey);
Determine if SortedList contains a specific key
5.public virtual bool ContainsKey (Objectkey);
Determines whether the SortedList contains a specific key, with contains (object key);
6.public virtual bool Containsvalue (objectvalue);
Determine if SortedList contains a specific value
These three functions are exactly the same as those of Hashtable
How to use C # SortedList