SortedList类表示按键排序并可按键和索引访问的键和值对的集合。
排序列表是数组和哈希表的组合,它包含可以使用键或索引访问的元素列表。如果使用索引访问项,则它是ArrayList;如果使用键访问项,则它是Hashtable。
下表列出了SortedList类-的一些常用属性
Sr.No. | Property & 描述 |
---|---|
1 |
Capacity 获取或设置SortedList的容量。 |
2 |
Count 获取SortedList中包含的元素数。 |
3 |
IsFixedSize 获取一个值,该值指示SortedList是否具有固定大小。 |
4 |
IsReadOnly 获取一个值,该值指示SortedList是否为只读。 |
5 |
Item 获取和设置与SortedList中的特定键关联的值。 |
6 |
Keys 获取SortedList中的键。 |
7 |
Values 获取SortedList中的值。 |
下表列出了SortedList类-的一些常用方法
Sr.No. | Method & 描述 |
---|---|
1 |
public virtual void add(object key,object value); 将具有指定键和值的元素添加到SortedList中。 |
2 |
public virtual void clear(); 从SortedList中删除所有元素。 |
3 |
public virtual bool ContainsKey(object key); 确定SortedList是否包含特定键。 |
4 |
public virtual bool ContainsValue(object value); 确定SortedList是否包含特定值。 |
5 |
public virtual object GetByIndex(int index); 获取SortedList的指定索引处的值。 |
6 |
public virtual object getkey(int index); 获取SortedList的指定索引处的键。 |
7 |
public virtual IList GetKeyList(); 获取SortedList中的键。 |
8 |
public virtual IList GetValueList(); 获取SortedList中的值。 |
9 |
public virtual int IndexOfKey(Object Key); 返回SortedList中指定键的从零开始的索引。 |
10 |
public virtual int IndexOfValue(object value); 返回SortedList中指定值的第一个匹配项的从零开始的索引。 |
11 |
public virtual void remove(Object Key); 从SortedList中移除具有指定键的元素。 |
12 |
public virtual void RemoveAt(Int Index); 移除SortedList的指定索引处的元素。 |
13 |
public virtual void TrimToSize(); 链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-sortedlist.html 来源:LearnFk无涯教程网 将容量设置为SortedList中的实际元素数。 |
以下示例演示了概念
using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { SortedList sl = new SortedList(); sl.Add("001", "Zara Ali"); sl.Add("002", "Abida Rehman"); sl.Add("003", "Joe Holzner"); sl.Add("004", "Mausam Benazir Nur"); sl.Add("005", "M. Amlan"); sl.Add("006", "M. Arif"); sl.Add("007", "Ritesh Saikia"); if (sl.ContainsValue("Nuha Ali")) { Console.WriteLine("This student name is already in the list"); } else { sl.Add("008", "Nuha Ali"); } //get a collection of the keys. ICollection key = sl.Keys; foreach (string k in key) { Console.WriteLine(k + ": " + sl[k]); } } } }
编译并执行上述代码时,将生成以下输出-
001: Zara Ali 002: Abida Rehman 003: Joe Holzner 004: Mausam Banazir Nur 005: M. Amlan 006: M. Arif 007: Ritesh Saikia 008: Nuha Ali
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
PPT设计进阶 · 从基础操作到高级创意 -〔李金宝(Bobbie)〕