SortedList<TKey、TValue>和SortedList是集合类,它们可以存储基于关联的IComparer实现按键排序的键值对。例如,如果键是原始类型,则按键的升序排序。
C# 支持泛型和非泛型 SortedList。建议使用泛型 SortedList < TKey,TValue > ,因为它比非泛型 SortedList 执行得更快,更不容易出错。
SortedList<TKey,TValue>是按键排序的键值对数组。
元素添加后立即对其进行排序。根据 IComparer < T > 按升序对原始类型键进行排序,并对对象键进行排序。
属于 System.Collection.Generic 命名空间。
键必须是唯一的,并且不能为 null。
值可以为null或重复。
可以通过在索引器mySortedList[key]中传递相关键来访问值
包含类型为 KeyValuePair <TKey,TValue>的元素
它比 SortedDictionary<TKey,TValue> 使用的内存更少。
排序后的数据检索速度更快,而 SortedDictionary<TKey,TValue>插入和删除键值对的速度更快。
以下示例演示了如何创建泛型SortedList<TKey, TValue>,并在其中添加键值对。
//整型键列表,字符串值
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");
//以下将引发异常
//numberNames.Add("Three", 3); //编译时错误:键必须为int类型
//numberNames.Add(1, "One"); //运行时异常:键重复
//numberNames.Add(null, "Five");//运行时异常:键不能为null
在上面的实例中,一个泛型的 SortedList<TKey,TValue> 对象是通过指定它要存储的键和值的类型来创建的。SortedList<int,string> 将存储int类型的键和string类型的值。
Add()方法用于在 SortedList 中添加单个键值对。键不能为null或重复。如果存在,它将抛出运行时异常。值可以重复,如果类型可以为null,则可以为null。
SortedList实例化时,使用collection-initializer语法初始化具有多个键值对,如下所示。
//创建字符串键,字符串值的SortedList
//使用collection-initializer语法
SortedList<string,string> cities = new SortedList<string,string>()
{
{"London", "UK"},
{"New York", "USA"},
{ "Mumbai", "India"},
{"Johannesburg", "South Africa"}
};
添加SortedList键值对后,将按键的升序重新排列键值对。下面的示例使用foreach循环显示所有键和值。
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[key]中 指定一个键,以获取或设置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]); //运行时 KeyNotFoundException
numberNames[2] = "TWO"; //更新值
numberNames[4] = "Four"; //如果键不存在,则添加新的键值
上面,numberNames[10] 将抛出一个KeyNotFoundException因为指定的键10在 sortedlist 中不存在的原因。为防止此异常,请使用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循环迭代SortedList,请使用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
使用 Remove(key) 和 RemoveAt(index) 方法从 SortedList 中删除键值对。
SortedList<int,string> numberNames = new SortedList<int,string>()
{
{3, "Three"},
{1, "One"},
{2, "Two"},
{5, "Five"},
{4, "Four"}
};
numberNames.Remove(1);//移除键1对
numberNames.Remove(10);//移除键1对,如果不存在,则没有错误
numberNames.RemoveAt(0);//从索引0删除键值对
//numberNames.RemoveAt(10);//运行时异常: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层次结构。
可在docs.microsoft.com上了解有关SortedList方法和属性的更多信息