C# 三十三、SortedList(排序列表)

SortedList 类:

  • 代表了一系列按照键来排序的键/值对,这些键值对可以通过键和索引来访问。
  • 排序列表是数组和哈希表的组合。它包含一个可使用键或索引访问各项的列表。如果您使用索引访问各项,则它是一个动态数组(ArrayList),如果您使用键访问各项,则它是一个哈希表(Hashtable)。集合中的各项总是按键值排序。

定义

几种基本的语法格式:

一、新实例初始化 System.Collections.SortedList 类为空,具有默认初始容量,并且根据排序 System.IComparable 接口由添加到每个键实现System.Collections.SortedList 对象。

SortedList  标识符 = new SortedList();

二、新实例初始化 System.Collections.SortedList 类为空,具有指定的初始容量,并且根据排序 System.IComparable接口由添加到每个键实现 System.Collections.SortedList 对象。

SortedList  标识符 = new SortedList(5);

三、新实例初始化 System.Collections.SortedList 类,该类包含从指定字典复制的元素已复制的元素数相同的初始容量并根据排序 System.IComparable 由每个键实现的接口。

SortedList 标识符 = new SortedList(指定的要被复制元素的字典);

SortedList 类的一些常用的属性

public virtual bool IsReadOnly { get; }

官方摘要:获取一个值,该值指示是否 System.Collections.SortedList 对象是只读的。

返回结果:bool值:如果 System.Collections.SortedList 是只读的,则为 true;否则为 false。 默认值为 false。

简单理解:判断是否只读。

代码示例:

SortedList sortedList = new SortedList();
Console.WriteLine(sortedList.IsReadOnly);

--->
False

public virtual bool IsFixedSize { get; }

官方摘要:获取一个值,该值指示是否 System.Collections.SortedList 对象具有固定的大小。

返回结果:bool值:如果System.Collections.SortedList 对象具有固定大小;则为 true;否则为 false。 默认值为 false。

简单理解:判断是否具有固定大小。

代码示例:

SortedList sortedList = new SortedList();

Console.WriteLine(sortedList.IsFixedSize);

--->
False

public virtual int Count { get; }

官方摘要:获取System.Collections.SortedList 对象中包含的元素数。

返回结果:System.Collections.SortedList 对象中包含的元素数。

简单理解:获取 SortedList 中的元素个数。

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");
sortedList.Add(4, "d");

Console.WriteLine(sortedList.Count);

--->
4

public virtual int Capacity { get; set; }

官方摘要:获取或设置 System.Collections.SortedList 对象的容量。

返回结果:System.Collections.SortedList 对象可以包含的元素数目 。

简单理解:Capacity 获取或设置 SortedList 的容量。

代码示例:

SortedList sortedList = new SortedList(5);

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");
sortedList.Add(4, "d");

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

--->
4
5

SortedList 类的一些常用的方法

public virtual void Add(object key, object value);

官方摘要:添加一个带有指定的键和值的元素到 System.Collections.SortedList 对象。

参数说明:

  • key:要添加的元素的键。
  • value:要添加的元素的值。 该值可以为 null。

简单理解:Capacity 获取或设置 SortedList 的容量。

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

foreach (DictionaryEntry item in sortedList)
{
    Console.WriteLine(item.Key);
    Console.WriteLine(item.Value);
}

--->
1
a
2
b
3
c

public virtual void Clear();

官方摘要:从 System.Collections.SortedList 对象中移除所有元素。

简单理解:从 SortedList 中移除所有的元素。

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");

sortedList.Clear();

sortedList.Add(3, "c");

foreach (DictionaryEntry item in sortedList)
{
    Console.WriteLine(item.Key);
    Console.WriteLine(item.Value);
}

--->
3
c

public virtual bool Contains(object key);

官方摘要:确定是否 System.Collections.SortedList 对象包含特定键。

参数说明:

  • key:要在 System.Collections.SortedList 对象中查找的键。

返回结果:如果 System.Collections.SortedList 对象包含具有指定的元素 key; 则为true,否则为 false。

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

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

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

--->
True

public virtual bool ContainsKey(object key);

官方摘要:确定是否 System.Collections.SortedList 对象包含特定键。

参数说明:

  • key:要在 System.Collections.SortedList 对象中查找的键。

返回结果:如果 System.Collections.SortedList 对象包含具有指定的元素 key; 则为true,否则为 false。

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

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

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

--->
True

public virtual bool ContainsValue(object value);

官方摘要:确定是否 System.Collections.SortedList 对象包含特定值。

参数说明:

  • value:要在 System.Collections.SortedList 对象中查找的值。

返回结果:如果 System.Collections.SortedList 对象包含具有指定的元素 value; 则为true,否则为 false。

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

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

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

--->
True

public virtual void CopyTo(Array array, int arrayIndex);

官方摘要:副本 System.Collections.SortedList 元素到一维 System.Array 对象,该数组中的指定索引处开始。

参数说明:

  • array:一维 System.Array 对象,从System.Collections.SortedList复制System.Collections.DictionaryEntry到该目标对象。  System.Array 必须具有从零开始的索引。
  • arrayIndex:array 中从零开始的索引,从此处开始复制。

简单理解:复制元素到一维数组。

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

DictionaryEntry[] arr = new DictionaryEntry[5];

sortedList.CopyTo(arr,0);

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

--->
1
2
3


public virtual object GetByIndex(int index);

官方摘要:获取System.Collections.SortedList 对象的指定索引处的值。

参数说明:

  • index:要获取的值的从零开始索引。

返回结果:指定System.Collections.SortedList 对象索引处的值。

简单理解:通过下标查找值。

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

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

--->
c

public virtual object GetKey(int index);

官方摘要:获取System.Collections.SortedList 对象指定索引处的键。

参数说明:

  • index:要获取的从零开始的索引的键。

返回结果:System.Collections.SortedList 对象指定索引处的键。

简单理解:通过下标查找键。

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

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

--->
3

public virtual IList GetKeyList();

官方摘要:获取System.Collections.SortedList 对象中的键。

返回结果:System.Collections.IList 对象,其中包含System.Collections.SortedList 对象中的键。

简单理解:获取所有键。

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

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

--->
1
2
3

public virtual IList GetValueList();

官方摘要:获取System.Collections.SortedList 对象中的值。

返回结果:System.Collections.IList 对象,其中包含System.Collections.SortedList 对象中的值。

简单理解:获取所有值。

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

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

--->
a
b
c

public virtual int IndexOfKey(object key);

官方摘要:返回System.Collections.SortedList 对象中从零开始的指定键的索引。

参数说明:

  • key:要在 System.Collections.SortedList 对象中查找的键。

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

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

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

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

--->
0

public virtual int IndexOfValue(object value);

官方摘要:返回System.Collections.SortedList 对象中从零开始的指定值的索引。

参数说明:

  • value:要在System.Collections.SortedList 对象中找到的值。 该值可以为 null。

返回结果:如果System.Collections.SortedList 对象中找到value,返回从零开始的索引; 否则为-1。

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

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

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

--->
2

public virtual void Remove(object key);

官方摘要:移除System.Collections.SortedList 对象带有指定键的元素 。

参数说明:

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

简单理解:通过键删除元素。

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

sortedList.Remove(1);

foreach (DictionaryEntry item in sortedList)
{
    Console.WriteLine(item.Key);
    Console.WriteLine(item.Value);
}

--->
2
b
3
c

public virtual void RemoveAt(int index);

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

参数说明:

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

简单理解:通过下标删除元素。

代码示例:

SortedList sortedList = new SortedList();

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

sortedList.RemoveAt(1);

foreach (DictionaryEntry item in sortedList)
{
    Console.WriteLine(item.Key);
    Console.WriteLine(item.Value);
}

--->
1
a
3
c

public virtual void SetByIndex(int index, object value);

官方摘要:替换System.Collections.SortedList 对象中的特定索引处的值。

参数说明:

  • index:在该位置保存从零开始的索引。
  • value:System.Object, 要保存到 System.Collections.SortedList 对象对应索引处的值。 该值可以为 null。

简单理解:通过下标更改值。

代码示例:

SortedList sortedList = new SortedList(8);

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

sortedList.SetByIndex(0,"A");

foreach (DictionaryEntry item in sortedList)
{
    Console.WriteLine(item.Key);
    Console.WriteLine(item.Value);
}

--->
1
A
2
b
3
c

public virtual void TrimToSize();

官方摘要:将容量设置为System.Collections.SortedList 对象中的元素的实际数目。

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

代码示例:

SortedList sortedList = new SortedList(8);

sortedList.Add(1, "a");
sortedList.Add(2, "b");
sortedList.Add(3, "c");

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

sortedList.TrimToSize();

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

--->
3
8
3
3

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值