C# 三十九、SortedList

SortedList<K,V> 

  • 表示基于相关的 System.Collections.Generic.IComparer`1 实现按键进行排序的键/值对的集合。
  • 类型参数:
    • TKey:集合中的键的类型。
    • TValue:集合中值的类型。

定义 

几种基本的语法格式:

一:初始化 System.Collections.Generic.SortedList`2 类的新实例,该示例为空且具有默认的初始容量,并使用默认的 System.Collections.Generic.IComparer`1。

   SortedList<键数据类型, 值数据类型> 标识符 = new SortedList<键数据类型, 值数据类型>();

二:初始化 System.Collections.Generic.SortedList`2 类的新实例,该示例为空且具有指定的初始容量,并使用默认的 System.Collections.Generic.IComparer`1。

  SortedList<键数据类型, 值数据类型> 标识符 = new SortedList<键数据类型, 值数据类型>(可包含的初始元素数);

三:初始化 System.Collections.Generic.SortedList`2 类的新实例,该实例包含从指定的 System.Collections.Generic.IDictionary`2中复制的元素,其容量足以容纳所复制的元素数并使用默认的 System.Collections.Generic.IComparer`1。

  SortedList<键数据类型, 值数据类型> 标识符 = new SortedList<键数据类型, 值数据类型>(复制元素到新列表的源字典);

 SortedList<TKey, TValue> 类的一些常用的属性

 public int Capacity { get; set; }

官方摘要:获取或设置 System.Collections.Generic.SortedList`2 可包含的元素数。

返回结果:System.Collections.Generic.SortedList`2 可包含的元素数。

简单理解:获取可包含的元素数目。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);
Console.WriteLine(sortedList.Capacity);
--->
5

 public int Count { get; }

官方摘要:获取包含在 System.Collections.Generic.SortedList`2 中的键/值对的数目。

返回结果:包含在 System.Collections.Generic.SortedList`2 中的键/值对的数目。

简单理解:获取实际包含的元素数目。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);
Console.WriteLine(sortedList.Count);
--->
0

SortedList<TKey, TValue> 类的一些常用的方法

 public void Add(TKey key, TValue value);

官方摘要:将带有指定键和值的元素添加到 System.Collections.Generic.SortedList`2 中。

参数说明:

  • key:要添加的元素的键。
  • value:要添加的元素的值。 对于引用类型,该值可以为 null。

简单理解:添加元素。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);

sortedList.Add(1, "aa");
sortedList.Add(2, "bb");
sortedList.Add(3, "cc");

foreach (var item in sortedList)
{
    Console.WriteLine(item);
}

--->
[1, aa]
[2, bb]
[3, cc]

public void Clear();

官方摘要:从 System.Collections.Generic.SortedList`2 中移除所有元素。

简单理解:移除所有元素。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);

sortedList.Add(1, "aa");
sortedList.Add(2, "bb");
sortedList.Add(3, "cc");

sortedList.Clear();

sortedList.Add(4, "dd");

foreach (var item in sortedList)
{
    Console.WriteLine(item);
}

--->
[4, dd]

public bool ContainsKey(TKey key);

官方摘要:确定 System.Collections.Generic.SortedList`2 是否包含特定键。

参数说明:

  • key:要在 System.Collections.Generic.SortedList`2 中定位的键。

返回结果:如果System.Collections.Generic.SortedList`2包含具有指定键的元素,则为true;否则为 false。

简单理解:判断是否包含指定键。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);

sortedList.Add(1, "aa");
sortedList.Add(2, "bb");
sortedList.Add(3, "cc");

Console.WriteLine(sortedList.ContainsKey(2));

--->
True

 public bool ContainsValue(TValue value);

官方摘要:确定 System.Collections.Generic.SortedList`2 是否包含特定值。

参数说明:

  • value:要在 System.Collections.Generic.SortedList`2 中定位的值。 对于引用类型,该值可以为 null。

返回结果:如果System.Collections.Generic.SortedList`2包含具有指定值的元素,则为true;否则为 false。

简单理解:判断是否包含指定值。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);

sortedList.Add(1, "aa");
sortedList.Add(2, "bb");
sortedList.Add(3, "cc");

Console.WriteLine(sortedList.ContainsValue("cc"));

--->
True

 public int IndexOfKey(TKey key);

官方摘要:在整个 System.Collections.Generic.SortedList`2 中搜索指定键并返回从零开始的索引。

参数说明:

  • key:要在 System.Collections.Generic.SortedList`2 中定位的键。

返回结果:如果找到,则为整个 key 中 System.Collections.Generic.SortedList`2 的从零开始的索引;否则为 -1。

简单理解:获取指定键的下标。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);

sortedList.Add(1, "aa");
sortedList.Add(2, "bb");
sortedList.Add(3, "cc");

Console.WriteLine(sortedList.IndexOfKey(1));

--->
0

 public int IndexOfValue(TValue value);

官方摘要:在整个 System.Collections.Generic.SortedList`2 中搜索指定的值,并返回第一个匹配项的从零开始的索引。

参数说明:

  • value:要在 System.Collections.Generic.SortedList`2 中定位的值。 对于引用类型,该值可以为 null。

返回结果:如果找到,则为整个value中System.Collections.Generic.SortedList`2 第一个匹配项的从零开始的索引;否则为  -1。

简单理解:获取指定值的下标。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);

sortedList.Add(1, "aa");
sortedList.Add(2, "bb");
sortedList.Add(3, "cc");

Console.WriteLine(sortedList.IndexOfValue("cc"));

--->
2

 public bool Remove(TKey key);

官方摘要:从 System.Collections.Generic.SortedList`2 中移除带有指定键的元素。

参数说明:

  • key:要移除的元素的键。

返回结果:如果该元素已成功移除,则为 true;否则为 false。 如果在原始System.Collections.Generic.SortedList`2中没有找到 key,则此方法也会返回false 。

简单理解:移除带有指定键的元素。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);

sortedList.Add(1, "aa");
sortedList.Add(2, "bb");
sortedList.Add(3, "cc");

Console.WriteLine(sortedList.Remove(1));

foreach (var item in sortedList)
{
    Console.WriteLine(item);
}

--->
True
[2, bb]
[3, cc]

 public void RemoveAt(int index);

官方摘要:移除 System.Collections.Generic.SortedList`2 的指定索引处的元素。

参数说明:

  • index:要移除的元素的从零开始的索引。

简单理解:移除指定下标的元素。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);

sortedList.Add(1, "aa");
sortedList.Add(2, "bb");
sortedList.Add(3, "cc");

sortedList.RemoveAt(1);

foreach (var item in sortedList)
{
    Console.WriteLine(item);
}

--->
[1, aa]
[3, cc]

public void TrimExcess();

官方摘要:如果元素数小于当前容量的 90%,将容量设置为 System.Collections.Generic.SortedList`2 中的实际元素数。

简单理解:将容量设置为实际元素数。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);

sortedList.Add(1, "aa");
sortedList.Add(2, "bb");
sortedList.Add(3, "cc");

Console.WriteLine(sortedList.Count);
Console.WriteLine(sortedList.Capacity);

sortedList.TrimExcess();

Console.WriteLine(sortedList.Count);
Console.WriteLine(sortedList.Capacity);

--->
3
5
3
3

public bool TryGetValue(TKey key, out TValue value);

官方摘要:获取与指定键关联的值。

参数说明:

  • key:要获取其值的键。
  • value:当此方法返回时,如果找到指定键,则返回与该键相关联的值;否则,将返回 value 参数的类型的默认值。 此参数未经初始化即被传递。

返回结果:如果System.Collections.Generic.SortedList`2包含具有指定键的元素,则为true;否则为 false。

简单理解:获取与指定键关联的值。

代码示例:

SortedList<int,string> sortedList = new SortedList<int, string>(5);

sortedList.Add(1, "aa");
sortedList.Add(2, "bb");
sortedList.Add(3, "cc");

string s;

sortedList.TryGetValue(1,out s);

Console.WriteLine(s);

--->
aa

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值