SortedList类是根据键对(键,值)对进行排序的集合。可以通过键和索引(从零开始的索引)访问这些对。它位于System.Collections命名空间下。
SortedList类的特征:
- 可以通过其键或索引来访问SortedList元素。
- SortedList对象在内部维护两个数组来存储列表的元素,即,一个数组用于键,另一个数组用于关联的值。
- 键不能为null,但值可以为null。
- SortedList对象的容量是SortedList可以容纳的元素数。
- SortedList不允许重复的键。
- 由于排序,对SortedList对象的操作往往比对Hashtable对象的操作要慢。
- 可以使用整数索引访问此集合中的元素。此集合中的索引从零开始。
建设者
Constructor | Description |
---|---|
SortedList() | Initializes a new instance of the SortedList class that is empty, has the default initial capacity, and is sorted according to the IComparable interface implemented by each key added to the SortedList object. |
SortedList(IComparer) | Initializes a new instance of the SortedList class that is empty, has the default initial capacity, and is sorted according to the specified IComparer interface. |
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. |
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. |
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. |
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 object. |
例子:
// C# Program to create a SortedList
using System;
using System.Collections;
class Geeks {
// Main Method
public static void Main(String[] args)
{
// Creating object of SortedList
// fslist is the SortedList object
SortedList fslist = new SortedList();
// Count property is used to get the
// number of key/value pairs in fslist
// It will give 0 as no pairs are present
Console.WriteLine(fslist.Count);
}
}
输出:
0
特性
Property | Description |
---|---|
Capacity | Gets or sets the capacity of a SortedList object. |
Count | Gets the number of elements contained in a SortedList object. |
IsFixedSize | Gets a value indicating whether a SortedList object has a fixed size. |
IsReadOnly | Gets a value indicating whether a SortedList object is read-only. |
IsSynchronized | Gets a value indicating whether access to a SortedList object is synchronized (thread safe). |
Item[Object] | Gets and sets the value associated with a specific key in a SortedList object. |
Keys | Gets the keys in a SortedList object. |
Values | Gets the values in a SortedList object. |
例子:
// C# Program to illustrate the
// properties of SortedList Class
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating an SortedList
SortedList mySortedList = new SortedList();
// Adding elements to SortedList
mySortedList.Add("1", "one");
mySortedList.Add("2", "two");
mySortedList.Add("3", "three");
mySortedList.Add("4", "four");
mySortedList.Add("5", "five");
// Checking if a SortedList
// object has a fixed size
Console.WriteLine(mySortedList.IsFixedSize);
// Checking if the created
// SortedList is read-only or not
Console.WriteLine(mySortedList.IsReadOnly);
}
}
输出:
False
False
方法
Method | Description |
---|---|
Add(Object, Object) | Adds an element with the specified key and value to a SortedList object. |
Clear() | Removes all elements from a SortedList object. |
Clone() | Creates a shallow copy of a SortedList object. |
Contains(Object) | Determines whether a SortedList object contains a specific key. |
ContainsKey(Object) | Determines whether a SortedList object contains a specific key. |
ContainsValue(Object) | Determines whether a SortedList object contains a specific value. |
CopyTo(Array, Int32) | Copies SortedList elements to a one-dimensional Array object, starting at the specified index in the array. |
Equals(Object) | Determines whether the specified object is equal to the current object. |
GetByIndex(Int32) | Gets the value at the specified index of a SortedList object. |
GetEnumerator() | Returns an IDictionaryEnumerator object that iterates through a SortedList object. |
GetHashCode() | Serves as the default hash function. |
GetKey(Int32) | Gets the key at the specified index of a SortedList object. |
GetKeyList() | Gets the keys in a SortedList object. |
GetType() | Gets the Type of the current instance. |
GetValueList() | Gets the values in a SortedList object. |
IndexOfKey(Object) | Returns the zero-based index of the specified key in a SortedList object. |
IndexOfValue(Object) | Returns the zero-based index of the first occurrence of the specified value in a SortedList object. |
MemberwiseClone() | Creates a shallow copy of the current Object. |
Remove(Object) | Removes the element with the specified key from a SortedList object. |
RemoveAt(Int32) | Removes the element at the specified index of a SortedList object. |
SetByIndex(Int32, Object) | Replaces the value at a specific index in a SortedList object. |
Synchronized(SortedList) | Returns a synchronized (thread-safe) wrapper for a SortedList object. |
ToString() | Returns a string that represents the current object. |
TrimToSize() | Sets the capacity to the actual number of elements in a SortedList object. |
范例1:
// C# code to remove all
// elements from a SortedList
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating an SortedList
SortedList mySortedList = new SortedList();
// Adding elements to SortedList
mySortedList.Add("1", "1st");
mySortedList.Add("2", "2nd");
mySortedList.Add("3", "3rd");
mySortedList.Add("4", "4th");
mySortedList.Add("5", "5th");
mySortedList.Add("6", "6th");
mySortedList.Add("7", "7th");
// Displaying number of elements
Console.WriteLine("Number of elements in SortedList is : "
+ mySortedList.Count);
// Displaying capacity
Console.WriteLine("capacity of SortedList is : "
+ mySortedList.Capacity);
// Removing all elements from SortedList
mySortedList.Clear();
// Displaying number of elements
Console.WriteLine("Number of elements in SortedList is : "
+ mySortedList.Count);
// Displaying capacity
Console.WriteLine("capacity of SortedList is : "
+ mySortedList.Capacity);
}
}
输出:
Number of elements in SortedList is : 7
capacity of SortedList is : 16
Number of elements in SortedList is : 0
capacity of SortedList is : 16
范例2:
// C# code to check if a SortedList
// object contains a specific value
using System;
using System.Collections;
class GFG {
// Driver code
public static void Main()
{
// Creating an SortedList
SortedList mySortedList = new SortedList();
// Adding elements to SortedList
mySortedList.Add("1", "1st");
mySortedList.Add("2", "2nd");
mySortedList.Add("3", "3rd");
mySortedList.Add("4", "4th");
// Checking if a SortedList object
// contains a specific value
Console.WriteLine(mySortedList.ContainsValue(null));
}
}
输出:
False
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.sortedlist?view=netframework-4.7.2