首页 > 编程笔记 > C#笔记 阅读:15

C# SortedList集合的用法(附带实例)

C# 中的 SortedList 是一个保持排序、数据不重复出现的排序列表集合。每个元素都由 <TKey, TValue> 组成,与字典对象相同,相当于创建此集合后,此集合会合动依键做排序。

C# SortedList对象的创建

如果只是创建 SortedList 对象,则可以使用下列语法:
SortedList<TKey, TValue> 对象名称 = new SortedList<TKey, TValue>();

如果要创建 SortedList 对象,同时还要设定此对象的容量,则可以增加 Int32 参数,详情可以参考下列语法:
SortedList<TKey, TValue> 对象名称 = new SortedList<TKey, TValue>(Int32);

如果要创建对象,同时设置初始化,则可以使用下列语法:
SortedList<TKey, TValue> 对象名称 = new SortedList<TKey, TValue> { 系列初始化值 }

例如:
SortedList<int, string> number = new SortedList<int, string>()
{
    {9, "nine" },
    {1, "one" },
    {6, "six" },
    {3, "three" }
};
foreach (KeyValuePair<int, string> n in number)
    Console.WriteLine($"Key:{n.Key}, Value:{n.Value}");

var season = new SortedList<string, string>()
{
    {"Spring", "春季"},
    {"Summer", "夏季"},
    {"Autumn", "秋季"},
    {"Winter", "冬季"}
};
foreach (var n in season)
    Console.WriteLine($"Key:{n.Key}, Value:{n.Value}");
执行结果为:

Key:1, Value:one
Key:3, Value:three
Key:6, Value:six
Key:9, Value:nine
Key:Autumn, Value:秋季
Key:Spring, Value:春季
Key:Summer, Value:夏季
Key:Winter, Value:冬季


如果要创建 SortedList 对象,同时将指定项目复制至此对象,可以使用下列语法:
SortedList<TKey, TValue> 对象名称 = new SortedList<TKey, TValue>(IDictionary<TKey, TValue>);

注意,上述 IDictionary 是源自 System.Collections 命名空间的接口,这个接口的功能是元素以 <键, 值> 组成,可以想成元素是字典数据。上述 IDictionary 是指字典的数据类型,可以作为指定项目复制到 SortedList 对象名称内。

【实例 1】创建 SortedList 对象,同时将字典数据复制至此对象。
var season = new SortedList<string, string>()
{
    {"Spring", "春季"},
    {"Summer", "夏季"},
    {"Autumn", "秋季"},
    {"Winter", "冬季"}
};
SortedList<string, string> sortS = new SortedList<string, string>(season);
foreach (var s in sortS)
    Console.WriteLine($"Key:{s.Key}, Value:{s.Value}");
执行结果为:

Key:Autumn, Value:秋季
Key:Spring, Value:春季
Key:Summer, Value:夏季
Key:Winter, Value:冬季

C# SortedList的属性

SortedList 的常用属性如下:
【实例 2】输出 SortedList 的元素个数,同时输出所有键与值。
SortedList<int, string> number = new SortedList<int, string>()
{
    {9, "nine" },
    {1, "one" },
    {6, "six" },
    {3, "three" }
};
Console.WriteLine($"元素个数:{number.Count}");
Console.WriteLine("元素键的内容如下:");
foreach (int n in number.Keys)
    Console.WriteLine($"Key:{n}");
Console.WriteLine("元素值的内容如下:");
foreach (string n in number.Values)
    Console.WriteLine($"Value:{n}");
执行结果为:

元素个数:4
元素键的内容如下:
Key:1
Key:3
Key:6
Key:9
元素值的内容如下:
Value:one
Value:three
Value:six
Value:nine

C# SortedList的方法

下表罗列了 SortedList 常用的方法:

方法名称 描述
SortedList<TKey, TValue>.Add(TKey, TValue) 将键值配对元素加入。
SortedList<TKey, TValue>.Clear() 删除 SortedList 对象的所有元素。
SortedList<TKey, TValue>.ContainsKey(TKey) 回传键是否存在。
SortedList<TKey, TValue>.Contains(TValue) 回传值是否存在。
SortedList<TKey, TValue>.Remove(TKey) 将含特定键的元素删除。
SortedList<TKey, TValue>.GetKeyAt(Int32) 取得特定索引的键。
SortedList<TKey, TValue>.GetValueAt(Int32) 取得特定索引的值。
SortedList<TKey, TValue>.IndexOfKey(TKey) 取得特定键的索引。
SortedList<TKey, TValue>.IndexOfValue(TValue) 取得特定值的索引。
SortedList<TKey, TValue>.TryGetValue(TKey, TValue) 取得指定键的值。

除了上述方法,还可以使用下列方式取得与设定 SortedList 配对值:
字典变量[Key] = xx;

【实例 3】以不同方式创建 SortedList 对象,然后删除含键 2 的元素,同时将输出结果做比较。
SortedList<int, string> num = new SortedList<int, string>();
num.Add(3, "Three");
num.Add(1, "One");
num[2] = "Two";    // 用不同方式创建元素
num[5] = "Five";
num[4] = "Four";
foreach (var n in num)
    Console.WriteLine($"{n.Key}:{n.Value}, 6}, ");
num.Remove(2);    // 删除成功
num.Remove(10);   // 键不存在,但是程序没有错误
Console.WriteLine("\n删除元素 2 后");
foreach (var n in num)
    Console.WriteLine($"{n.Key}:{n.Value}, 6}, ");
执行结果为:

1: One, 2: Two, 3: Three, 4: Four, 5: Five,
删除元素 2 后
1: One, 3: Three, 4: Four, 5: Five,


【实例 4】Contains() 和 TryGetValue() 函数的应用。
SortedList<int, string> num = new SortedList<int, string>();
num.Add(3, "Three");
num.Add(1, "One");
num[2] = "Two";    // 用不同方式创建元素
num[5] = "Five";
num[4] = "Four";
if (!num.ContainsKey(9))    // 如果不含 Key 是 9
{
    num[9] = "Nine";    // 创建此元素
}
string result;
if (num.TryGetValue(9, out result))
    Console.WriteLine($"9:{result}");
执行结果

9:Nine

相关文章