1、SortedList定義
System.Collections.SortedList類表示鍵/值對的集合,這些鍵值對按鍵排序並可按照鍵和索引訪問。SortedList
在內部維護兩個數組以存儲列表中的元素;即,一個數組用於鍵,另一個數組用於相關聯的值。每個元素都是一個可作為 DictionaryEntry
對象進行訪問的鍵/值對。鍵不能為null,但值可以。
2.優點
1、SortedList 允許通過相關聯鍵或通過索引對值進行訪問,可提供更大的靈活性。
2、可根據需要自動增大容量。
3. SortedList的構造器
構造器函數 |
注釋 |
初始化 SortedList 類的新實例,該實例為空、具有默認初始容量並根據 IComparable |
|
初始化 SortedList 類的新實例,該實例包含從指定字典復制的元素、具有與所復制的元素數相同的初始容量並根據由每個鍵實現的 IComparable |
|
初始化 SortedList 類的新實例,該實例為空、具有指定的初始容量並根據 IComparable 接口(此接口由添加到 SortedList |
4、SortedList的屬性
屬性名 |
注釋 |
獲取 SortedList 中包含的元素數。 |
|
獲取一個值,該值指示 SortedList是否具有固定大小。 |
|
獲取一個值,該值指示 SortedList是否為只讀。 |
|
獲取包含 SortedList中的鍵集合 |
|
獲取包含 SortedList中的值的 集合 |
|
獲取或設置 SortedList |
5. SortedList的方法
方法名 |
注釋 |
Void Add(object |
將帶有指定鍵和值的元素添加到 SortedList。 |
Void Clear() |
從 SortedList 中移除所有元素。 |
Bool Contains(object |
確定 SortedList 是否包含特定鍵。 |
Bool ContainsKey(object |
確定 SortedList 是否包含特定鍵。 |
Bool ContainsValue(object |
確定 SortedList 是否包含特定值。 |
Void Remove(object |
從 SortedList 中移除帶有指定鍵的元素。 |
Void CopyTo(Array |
將 SortedList 元素復制到一維 Array |
Clone() |
創建 SortedList的淺表副本。 |
Object GetKey(int |
獲取 SortedList 的指定索引處的鍵。 |
Object GetByIndex(int |
獲取 SortedList 的指定索引處的值。 |
IDictionaryEnumerator GetEnumerator() |
返回循環訪問 SortedList 的 IDictionaryEnumerator。 |
IList GetKeyList() |
獲取 SortedList 中的鍵。 |
IList GetValueList() |
獲取 SortedList 中的值。 |
Int IndexOfKey(object |
返回 SortedList 中指定鍵的從零開始的索引。 |
Int IndexOfValue(object |
返回指定的值在 SortedList 中第一個匹配項的從零開始的索引。 |
Void RemoveAt(int |
移除 SortedList 的指定索引處的元素。 |
Void SetByIndex(int |
替換 SortedList 中指定索引處的值。 |
Void TrimToSize() |
將容量設置為 SortedList 中元素的實際數目。 |
6、SortedList的使用示例

public class TestSortedList
{
public static void Main()
{
// 創建一個SortedList對象
SortedList mySL = new SortedList();
mySL.Add( " First " , " Hello " );
mySL.Add( " Second " , " World " );
mySL.Add( " Third " , " ! " );
// 列舉SortedList的屬性、鍵、值
Console.WriteLine( " mySL " );
Console.WriteLine( " Count: {0} " , mySL.Count);
Console.WriteLine( " Capacity: {0} " , mySL.Capacity);
Console.WriteLine( " Keys and Values: " );
PrintIndexAndKeysAndValues(mySL);
#region SortedList獲得鍵、值列表
SortedList mySL1 = new SortedList();
mySL1.Add( 1.3 , " fox " );
mySL1.Add( 1.4 , " jumped " );
mySL1.Add( 1.5 , " over " );
mySL1.Add( 1.2 , " brown " );
mySL1.Add( 1.1 , " quick " );
mySL1.Add( 1.0 , " The " );
mySL1.Add( 1.6 , " the " );
mySL1.Add( 1.8 , " dog " );
mySL1.Add( 1.7 , " lazy " );
// 獲得指定索引處的鍵和值
int myIndex = 3 ;
Console.WriteLine( " The key at index {0} is {1}. " , myIndex, mySL1.GetKey(myIndex));
Console.WriteLine( " The value at index {0} is {1}. " , myIndex, mySL1.GetByIndex(myIndex));
// 獲得SortedList中的鍵列表和值列表
IList myKeyList = mySL1.GetKeyList();
IList myValueList = mySL1.GetValueList();
// Prints the keys in the first column and the values in the second column.
Console.WriteLine( " \t-KEY-\t-VALUE- " );
for ( int i = 0 ; i < mySL1.Count; i ++ )
Console.WriteLine( " \t{0}\t{1} " , myKeyList[i], myValueList[i]);
#endregion
#region 為SortedList中的元素重新賦值
// Creates and initializes a new SortedList.
SortedList mySL2 = new SortedList();
mySL2.Add( 2 , " two " );
mySL2.Add( 3 , " three " );
mySL2.Add( 1 , " one " );
mySL2.Add( 0 , " zero " );
mySL2.Add( 4 , " four " );
// 打印顯示列表的鍵和值
Console.WriteLine( " The SortedList contains the following values: " );
PrintIndexAndKeysAndValues(mySL2);
// 獲得指定鍵的索引
int myKey = 2 ;
Console.WriteLine( " The key \ " { 0 }\ " is at index {1}. " , myKey, mySL2.IndexOfKey(myKey));
// 獲得指定值的索引
String myValue = " three " ;
Console.WriteLine( " The value \ " { 0 }\ " is at index {1}. " , myValue, mySL2.IndexOfValue(myValue));
// 重新設置指定索引處的值
mySL2.SetByIndex( 3 , " III " );
mySL2.SetByIndex( 4 , " IV " );
// 打印顯示列表的鍵和值
Console.WriteLine( " After replacing the value at index 3 and index 4, " );
PrintIndexAndKeysAndValues(mySL2);
#endregion
Console.ReadKey();
}
// 打印SortedList中的鍵和值
public static void PrintIndexAndKeysAndValues(SortedList myList)
{
Console.WriteLine( " \t-INDEX-\t-KEY-\t-VALUE- " );
for ( int i = 0 ; i < myList.Count; i ++ )
{
Console.WriteLine( " \t[{0}]:\t{1}\t{2} " , i, myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
7.備注
1、SortedList 的容量是 SortedList 可以保存的元素數。SortedList 的默認初始容量為 0。隨着元素添加到
SortedList 中,在需要時可以通過重新分配自動增加容量。可通過調用 TrimToSize方法
或通過顯式設置 Capacity
屬性減少容量。
2、SortedList 中不允許重復鍵。
3、SortedList的索引順序基於排序順序。當添加元素時,元素將按正確的排序順序插入
SortedList,同時索引會相應地進行調整。當移除元素時,索引也會相應地進行調整。因此,當在 SortedList
中添加或移除元素時,特定鍵/值對的索引可能會更改。
4.當不向集合中添加新元素,則調用TrimToSize方法可用於最小化集合的內存開銷。
5、通過設置 SortedList 中不存在的鍵值(例如,myCollection["myNonexistentKey"] =
myValue),還可以使用 Item 屬性添加新元素。但是,如果指定的鍵已經存在於 SortedList 中,則設置 Item 屬性將改寫舊值。相比之下,Add
方法不修改現有元素。
鍵不能為 空引用(在 Visual Basic 中為 Nothing),但值可以。若要區分由於未找到指定鍵而返回的 空引用(在
Visual Basic 中為 Nothing) 和由於指定鍵的值為 空引用(在 Visual Basic 中為 Nothing) 而返回的 空引用(在
Visual Basic 中為 Nothing),請使用 Contains
方法或 ContainsKey
方法確定列表中是否存在該鍵。