C# SortedList(排序列表)

SortedList<TKey、TValue>和SortedList是集合類,它們可以存儲(chǔ)基于關(guān)聯(lián)的IComparer實(shí)現(xiàn)按鍵排序的鍵值對(duì)。例如,如果鍵是原始類型,則按鍵的升序排序。

C# 支持泛型和非泛型 SortedList。建議使用泛型 SortedList < TKey,TValue > ,因?yàn)樗确欠盒?SortedList 執(zhí)行得更快,更不容易出錯(cuò)。

SortedList 特性

  • SortedList<TKey,TValue>是按鍵排序的鍵值對(duì)數(shù)組。

  • 元素添加后立即對(duì)其進(jìn)行排序。根據(jù) IComparer < T > 按升序?qū)υ碱愋玩I進(jìn)行排序,并對(duì)對(duì)象鍵進(jìn)行排序。

  • 屬于 System.Collection.Generic 命名空間。

  • 鍵必須是唯一的,并且不能為 null。

  • 值可以為null或重復(fù)。

  • 可以通過在索引器mySortedList[key]中傳遞相關(guān)鍵來訪問值

  • 包含類型為 KeyValuePair <TKey,TValue>的元素

  • 它比 SortedDictionary<TKey,TValue> 使用的內(nèi)存更少。

  • 排序后的數(shù)據(jù)檢索速度更快,而 SortedDictionary<TKey,TValue>插入和刪除鍵值對(duì)的速度更快。

創(chuàng)建SortedList

以下示例演示了如何創(chuàng)建泛型SortedList<TKey, TValue>,并在其中添加鍵值對(duì)。

//整型鍵列表,字符串值 
SortedList<int, string> numberNames = new SortedList<int, string>();
numberNames.Add(3, "Three");
numberNames.Add(1, "One");
numberNames.Add(2, "Two");
numberNames.Add(4, null);
numberNames.Add(10, "Ten");
numberNames.Add(5, "Five");

//以下將引發(fā)異常
//numberNames.Add("Three", 3); //編譯時(shí)錯(cuò)誤:鍵必須為int類型
//numberNames.Add(1, "One"); //運(yùn)行時(shí)異常:鍵重復(fù)
//numberNames.Add(null, "Five");//運(yùn)行時(shí)異常:鍵不能為null

在上面的實(shí)例中,一個(gè)泛型的 SortedList<TKey,TValue> 對(duì)象是通過指定它要存儲(chǔ)的鍵和值的類型來創(chuàng)建的。SortedList<int,string> 將存儲(chǔ)int類型的鍵和string類型的值。

Add()方法用于在 SortedList 中添加單個(gè)鍵值對(duì)。鍵不能為null或重復(fù)。如果存在,它將拋出運(yùn)行時(shí)異常。值可以重復(fù),如果類型可以為null,則可以為null。

SortedList實(shí)例化時(shí),使用collection-initializer語法初始化具有多個(gè)鍵值對(duì),如下所示。

//創(chuàng)建字符串鍵,字符串值的SortedList 
//使用collection-initializer語法
SortedList<string,string> cities = new SortedList<string,string>()
                                    {
                                        {"London", "UK"},
                                        {"New York", "USA"},
                                        { "Mumbai", "India"},
                                        {"Johannesburg", "South Africa"}
                                    };

添加SortedList鍵值對(duì)后,將按鍵的升序重新排列鍵值對(duì)。下面的示例使用foreach循環(huán)顯示所有鍵和值。

SortedList<int,string> numberNames = new SortedList<int,string>()
                                    {
                                        {3, "Three"},
                                        {5, "Five"},
                                        {1, "One"}
                                    };

Console.WriteLine("---初始鍵值--");

foreach(KeyValuePair<int, string> kvp in numberNames)
    Console.WriteLine("key: {0}, value: {1}", kvp.Key , kvp.Value );

numberNames.Add(6, "Six");
numberNames.Add(2, "Two");
numberNames.Add(4, "Four");

Console.WriteLine("---添加新鍵值后--");

foreach(var kvp in numberNames)
    Console.WriteLine("key: {0}, value: {1}", kvp.Key , kvp.Value );
輸出:
---初始鍵值--
key: 1, value: One
key: 3, value: Three
key: 5, value: Five
---添加新鍵值后--
key: 1, value: One
key: 2, value: Two
key: 3, value: Three
key: 4, value: Four
key: 5, value: Five
key: 6, value: Six

訪問SortedList

在索引器 SortedList[key]中 指定一個(gè)鍵,以獲取或設(shè)置SortedList中的值。

SortedList<int,string> numberNames = new SortedList<int,string>()
                                    {
                                        {3, "Three"},
                                        {1, "One"},
                                        {2, "Two"}
                                    };

Console.WriteLine(numberNames[1]); //輸出:One
Console.WriteLine(numberNames[2]); //輸出:Two
Console.WriteLine(numberNames[3]); //輸出:Three
//Console.WriteLine(numberNames[10]); //運(yùn)行時(shí) KeyNotFoundException

numberNames[2] = "TWO"; //更新值
numberNames[4] = "Four"; //如果鍵不存在,則添加新的鍵值

上面,numberNames[10] 將拋出一個(gè)KeyNotFoundException因?yàn)橹付ǖ逆I10在 sortedlist 中不存在的原因。為防止此異常,請(qǐng)使用ContainsKey()或TryGetValue()方法,如下所示。

SortedList<int, string> numberNames = new SortedList<int,string>()
                                    {
                                        {3, "Three"},
                                        {1, "One"},
                                        {2, "Two"}
                                    };
if(numberNames.ContainsKey(4)){
    numberNames[4] = "four";
}

int result;
if(numberNames.TryGetValue(4, out result))
    Console.WriteLine("Key: {0}, Value: {1}", 4, result);
輸出:
Key:4, Value: Four

如果要使用for循環(huán)迭代SortedList,請(qǐng)使用Keys和Values屬性。

SortedList<int, string> numberNames = new SortedList<int,string>()
                                    {
                                        {3, "Three"},
                                        {1, "One"},
                                        {2, "Two"}
                                    };
for (int i = 0; i < numberNames.Count; i++)
{
    Console.WriteLine("key: {0}, value: {1}", numberNames.Keys[i], numberNames.Values[i]);
}
輸出:
key: 1, value: One
key: 2, value: Two
key: 3, value: Three

從SortedList中刪除元素

使用 Remove(key) 和 RemoveAt(index) 方法從 SortedList 中刪除鍵值對(duì)。

SortedList<int,string> numberNames = new SortedList<int,string>()
                                    {
                                        {3, "Three"},
                                        {1, "One"},
                                        {2, "Two"},
                                        {5, "Five"},
                                        {4, "Four"}
                                    };
    
numberNames.Remove(1);//移除鍵1對(duì)
numberNames.Remove(10);//移除鍵1對(duì),如果不存在,則沒有錯(cuò)誤

numberNames.RemoveAt(0);//從索引0刪除鍵值對(duì) 
//numberNames.RemoveAt(10);//運(yùn)行時(shí)異常:ArgumentOutOfRangeException

foreach(var kvp in numberNames)
	Console.WriteLine("key: {0}, value: {1}", kvp.Key , kvp.Value );
輸出:
key: 3, value: Three
key: 4, value: Four
key: 5, value: Five

SortedList類層次結(jié)構(gòu)

下圖說明了SortedList層次結(jié)構(gòu)。

SortedList層次結(jié)構(gòu)

可在docs.microsoft.com上了解有關(guān)SortedList方法和屬性的更多信息

丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清