Sortedlist class
A set of key/value pairs. These keys and values are sorted by keys and indexes. Sortedlist is most suitable for sorting key/value pairs. sortedlist is a mixture of hashtable and array. When the item indexer attribute is used to access an element according to the key of the element, its behavior is similar to hashtable. When getbyindex or setbyindex is used to access an element based on the element index, the behavior is similar to array.
Sortedlist maintains two arrays internally to store the arrays in the list. That is, one array is used for keys, and the other array is used for associated values. 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 can be.
Sortedlist capacity is the number of elements available in the list. With the addition of elements to sortedlist, capacity increases automatically by reallocation as needed. You can call trimtosize or explicitly set the capacity attribute to reduce the capacity.
Sortedlist elements are implemented according to the specific icomparer implementation (specified when sortedlist is created) or the icomparable implementation provided by the key itself and sorted by the key. In either case, sortedlist does not allow duplicate keys.
The index order is based on the sorting order. When an element is added, the element is inserted to sortedlist in the correct order, and the index is adjusted accordingly. If an element is removed, the index is adjusted accordingly. Therefore, when an element is added or removed in sortedlist, the index of a specific key/value pair may be changed.
Because sorting is required, the operation on sortedlist is better than that on hashtable.
On the operation is slow. However, sortedlist allows access to values through related keys or indexes, providing greater flexibility.
I. Add or delete
1. Public Virtual void add (Object key, object value );
The index in this set starts from scratch.
Add the elements with the specified key and value to sortedlist.
By setting the value of a key that does not exist in sortedlist, item
Attribute can also be used to add new elements. For example, mycollection ["mynonexistentkey"] = myvalue. However, for example
If the specified key already exists in sortedlist, setting the item attribute will overwrite the old value. In contrast, the Add Method
Do not modify existing elements.
Sortedlist slist = new sortedlist ();
Slist. Add (1, "D ");
Slist. Add (2, "C ");
Slist. Add (3, "B ");
Slist. Add (4, "");
// The result is d c B A, so we can see that the key is sorted, not the value is sorted.
Dropdownlist3.datasource = slist;
Dropdownlist3.datatextfield = "key ";
Dropdownlist3.datavaluefield = "value ";
Dropdownlist3.databind ();
2. Public Virtual void remove (Object key );
Remove elements with specified keys from sortedlist
If sortedlist does not contain elements with the specified key, sortedlist remains unchanged. No exception
Sortedlist slist = new sortedlist ();
Slist. Add (1, "D ");
Slist. Add (2, "C ");
Slist. Add (3, "B ");
Slist. Add (4, "");
// Slist. Remove ("B"); error. Delete by key instead of 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 elements at the specified index of sortedlist.
Sortedlist slist = new sortedlist ();
Slist. Add (1, "D ");
Slist. Add (2, "C ");
Slist. Add (3, "B ");
Slist. Add (4, "");
Slist. removeat (3); // Delete [4, "a"]. The parameter here is the index number, not the key value,
// Unlike slist. Remove (3), slist. Remove (3) deletes [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. Capacity remains unchanged. To reset the capacity of sortedlist, call trimtosize or directly set the capacity attribute. If this parameter is left blank, sortedlist sets the capacity of sortedlist to the default capacity instead of zero.
II. Index-related operations
1. Public Virtual void setbyindex (INT index, object value );
Replace the value of the specified index in sortedlist.
Sortedlist slist = new sortedlist ();
Slist. Add (1, "D ");
Slist. Add (2, "C ");
Slist. Add (3, "B ");
Slist. Add (4, "");
Slist. setbyindex (1, "dddddddd"); // 1 is the index. If count <2, an error occurs, that is, it must exist.
// Slist [2] = "dddddd"; this phenomenon does not exist,
// That is, the slist [2] = "dddddd" is
// If the key exists in the modified value and does not exist, add
Dropdownlist3.datasource = slist;
Dropdownlist3.datatextfield = "key ";
Dropdownlist3.datavaluefield = "value ";
Dropdownlist3.databind ();
2. Public Virtual Object getbyindex (INT index );
Obtains the value of a specified index of sortedlist.
Index must be smaller than count; otherwise, an error occurs.
Sortedlist slist = new sortedlist ();
Slist. Add (1, "D ");
Slist. Add (2, "C ");
Slist. Add (3, "B ");
Slist. Add (4, "");
// 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 (Object key );
Returns the secondary index of the specified key in sortedlist.
This is not what hashtable does, because hashtable does not have the concept of order, and its sorting is internal.
4. Public Virtual int indexofvalue (object value );
Returns the index of the first matched item in sortedlist.
This is not what hashtable does, because hashtable does not have the concept of order, and its sorting is internal.
Sortedlist slist = new sortedlist ();
Slist. Add (1, "D ");
Slist. Add (2, "C ");
Slist. Add (3, "B ");
Slist. Add (4, "");
Slist. Add (5, "D ");
Int nindex = 0;
Nindex = slist. indexofkey (1); // 0
Nindex = slist. indexofvalue ("D"); // two matching values are returned. The first matching value is returned, so it is 0.
3. Others
1. Public Virtual Object getkey (INT index );
Obtains the key at the specified index of sortedlist.
This is also impossible for hashtable.
2. Public Virtual ilist getkeylist ();
Get the key in sortedlist
Sortedlist slist = new sortedlist ();
Slist. Add (1, "D ");
Slist. Add (2, "C ");
Slist. Add (3, "B ");
Slist. Add (4, "");
Slist. Add (5, "D ");
Label3.text = "";
Ilist = slist. getkeylist ();
For (INT I = 0; I <slist. Count; I ++)
{
Label3.text + = ilist [I]. tostring ();
Label3.text + = "";
}
Note: The ilist interface indicates a group of objects that can be accessed separately by index. There is an item attribute. in C #, It is the indexer.
3. Public Virtual ilist getvaluelist ();
Get the value in sortedlist
4. Public Virtual bool contains (Object key );
Determine whether sortedlist contains a specific key
5. Public Virtual bool containskey (Object key );
Determine whether sortedlist contains a specific key
Exactly the same as contains (Object key );
6. Public Virtual bool containsvalue (object value );
Determine whether sortedlist contains a specific value
The above three functions are exactly the same as those of hashtable.
7. Public Virtual void trimtosize ();
Set capacity to the actual number of elements in sortedlist