有序列表(SortedList),也稱為序列。他可以對列表中每個元素的插入位置進行精確地控制。 可以根據元素的在列表中的位置訪問元素,並搜索列表中的元素。 列表允許重復的元素。
有序列表是數組和哈希表的組合。 它包含一個可使用鍵或索引訪問各項的列表。 如果您使用索引訪問各項,則它是一個動態數組(ArrayList),如果您使用鍵訪問各項,則它是一個哈希表(Hashtable)。
有序列表集合中的各項總是按鍵值排序。
泛型有序列表SortedList<TKey,TValue>類
如果需要基於鍵對所需集合排序,就可以使用SortedList<TKey,TValue>類。這個類按照鍵給元素排序。這個集合中的值和鍵都可以使用任何類型。定義為鍵的自定義類型需要實現IComparer<T>接口,用於給列表中的元素排序。
使用構造函數創建一個有序列表,在用Add方法添加:
var books = new SortedList<string, string>();
books.Add("Professional WPF Programming", "978–0–470–04180–2");
books.Add("Professional ASP.NET MVC 3", "978–1–1180–7658–3");
還可以使用索引器將元素添加到列表中
books["Beginning Visual C# 2010"] = "978–0–470-50226-6";
books["Professional C# 4 and .NET 4"] = "978–0–470–50225–9";
SortedList<TKey,TValue>有多個重載版本的構造函數。
可以使用foreach語句遍歷列表,枚舉器返回的元素是KeyValuePair<TKey,TValue>類型,其中包含了鍵和值:
foreach (KeyValuePair<string, string> book in books)
{
Console.WriteLine("{0}, {1}", book.Key, book.Value);
}
迭代語句會按鍵的順序顯示:
Beginning Visual C# 2010, 978–0–470-50226-6
Professional ASP.NET MVC 3, 978–1–1180–7658–3
Professional C# 4 and .NET 4, 978–0–470–50225–9
Professional WPF Programming, 978–0–470–04180–2
也可以使用Values和Keys屬性訪問值和鍵:
foreach (string isbn in books.Values)
{
Console.WriteLine(isbn);
}
foreach (string title in books.Keys)
{
Console.WriteLine(title);
}
如果嘗試使用索引器訪問一個元素,但傳遞的鍵不存在,就會拋出異常。ContainsKey()方法,可以判斷所傳遞的鍵是否存在於集合中。TryGetValue該方法嘗試獲得指定鍵的值,如果指定的=鍵對應的值不存在,該方法不會拋出異常。
SortedList 類的方法和屬性
下表列出了 SortedList 類的一些常用的 屬性:
屬性 | 描述 |
---|---|
Capacity | 獲取或設置 SortedList 的容量。 |
Count | 獲取 SortedList 中的元素個數。 |
IsFixedSize | 獲取一個值,表示 SortedList 是否具有固定大小。 |
IsReadOnly | 獲取一個值,表示 SortedList 是否只讀。 |
Item | 獲取或設置與 SortedList 中指定的鍵相關的值。 |
Keys | 獲取 SortedList 中的鍵。 |
Values | 獲取 SortedList 中的值。 |
下表列出了 SortedList 類的一些常用的 方法:
方法名 | 描述 |
---|---|
public virtual void Add(object key,object value); | 向 SortedList 添加一個帶有指定的鍵和值的元素。 |
public virtual void Clear(); | 從 SortedList 中移除所有的元素。 |
public virtual bool ContainsKey(object key); | 判斷 SortedList 是否包含指定的鍵。 |
public virtual bool ContainsValue(object value); | 判斷 SortedList 是否包含指定的值。 |
public virtual object GetByIndex(int index); | 獲取 SortedList 的指定索引處的值。 |
public virtual object GetKey(int index); | 獲取 SortedList 的指定索引處的鍵。 |
public virtual IList GetKeyList(); | 獲取 SortedList 中的鍵。 |
public virtual IList GetValueList(); | 獲取 SortedList 中的值。 |
public virtual int IndexOfKey(object key); | 返回 SortedList 中的指定鍵的索引,索引從零開始。 |
public virtual int IndexOfValue( object value); | 返回 SortedList 中的指定值第一次出現的索引,索引從零開始。 |
public virtual void Remove(object key); | 從 SortedList 中移除帶有指定的鍵的元素。 |
public virtual void RemoveAt(int index); | 移除 SortedList 的指定索引處的元素。 |
public virtual void TrimToSize(); | 設置容量為 SortedList 中元素的實際個數。 |
有序列表范例
下面的范例演示了有序列表(SortedList)的屬性和函數:
using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { SortedList sl = new SortedList(); sl.Add("001", "Zara Ali"); sl.Add("002", "Abida Rehman"); sl.Add("003", "Joe Holzner"); sl.Add("004", "Mausam Benazir Nur"); sl.Add("005", "M. Amlan"); sl.Add("006", "M. Arif"); sl.Add("007", "Ritesh Saikia"); if (sl.ContainsValue("Nuha Ali")) { Console.WriteLine("This student name is already in the list"); } else { sl.Add("008", "Nuha Ali"); } // 獲取鍵的集合 ICollection key = sl.Keys; foreach (string k in key) { Console.WriteLine(k + ": " + sl[k]); } } } }
當上面的代碼被編譯和執行時,它會產生下列結果:
001: Zara Ali 002: Abida Rehman 003: Joe Holzner 004: Mausam Banazir Nur 005: M. Amlan 006: M. Arif 007: Ritesh Saikia 008: Nuha Ali