c#的sortedlist使用方法

时间:2023-09-22 04:58:13 

表示键/值对的集合,这些键和值按键排序并可按照键和索引访问。

SortedList最合适对一列健/值对 进行排序,在排序时,是对键进行排序,SortedList 是 Hashtable 和 Array 的混合。当使用 Item 索引器属性按照元素的键访问元素时,其行为类似于 Hashtable。当使用 GetByIndex 或 SetByIndex 按照元素的索引访问元素时,其行为类似于 Array。

SortedList 在内部维护两个数组以将数组存储到列表中;即,一个数组用于键,另一个数组用于相关联的值。每个元素都是一个可作为 DictionaryEntry 对象进行访问的键/值对。键不能为空引用(Visual Basic 中为 Nothing),但值可以。SortedList 的容量是列表可拥有的元素数。随着向 SortedList 中添加元素,容量通过重新分配按需自动增加。可通过调用 TrimToSize 或通过显式设置 Capacity 属性减少容量。SortedList 的元素将按照特定的 IComparer 实现(在创建SortedList 时指定)或按照键本身提供的 IComparable 实现并依据键来进行排序。不论在哪种情况下,SortedList 都不允许重复键。

索引顺序基于排序顺序。当添加元素时,元素将按正确的排序顺序插入 SortedList,同时索引会相应地进行调整。若移除了元素,索引也会相应地进行调整。因此,当在SortedList 中添加或移除元素时,特定键/值对的索引可能会更改。

由于要进行排序,所以在 SortedList 上操作比在 Hashtable 上操作要慢。但是,SortedList 允许通过相关联键或通过索引对值进行访问,可提供更大的灵活性。

一。添加删除

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

此集合中的索引从零开始。

将带有指定键和值的元素添加到 SortedList。

通过设置 SortedList 中不存在的键的值,Item 属性也可用于添加新元素。例如:myCollection["myNonexistentKey"] = myValue。但是,如果指定的键已经存在于 SortedList 中,则设置 Item 属性将改写旧值。相比之下,Add 方法不修改现有元素。


SortedList sList = new SortedList();
sList.Add(1,"d");
sList.Add(2,"c");
sList.Add(3,"b");
sList.Add(4,"a");
//结果为d c b a,所以可知是按键排序,而非值排序

DropDownList3.DataSource = sList;
DropDownList3.DataTextField = "Key";
DropDownList3.DataValueField = "Value";
DropDownList3.DataBind();

2。public virtual void Remove(object key);

从 SortedList 中移除带有指定键的元素
如果 SortedList 不包含带有指定键的元素,则 SortedList 保持不变。不引发异常


SortedList sList = new SortedList();
sList.Add(1,"d");
sList.Add(2,"c");
sList.Add(3,"b");
sList.Add(4,"a");
//sList.Remove("b");   错误,是按key删除,而非Value
sList.Remove(3);   //删除了[3,"b"]
DropDownList3.DataSource = sList;
DropDownList3.DataTextField = "Key";
DropDownList3.DataValueField = "Value";
DropDownList3.DataBind();

3。public virtual void RemoveAt(int index);

移除 SortedList 的指定索引处的元素。


SortedList sList = new SortedList();
sList.Add(1,"d");
sList.Add(2,"c");
sList.Add(3,"b");
sList.Add(4,"a");
sList.RemoveAt(3); //删除的是[4,"a"],这里的参数是索引号,而非键值,
//与sList.Remove(3)不同;   sList.Remove(3)删除了[3,"b"]

DropDownList3.DataSource = sList;
DropDownList3.DataTextField = "Key";
DropDownList3.DataValueField = "Value";
DropDownList3.DataBind();

4。public virtual void Clear();

从 SortedList 中移除所有元素Count 设置为零。Capacity 保持不变。若要重置 SortedList 的容量,请调用 TrimToSize或直接设置 Capacity 属性。截去空 SortedList 会将 SortedList 的容量设置为默认容量,而不是零

二。与索引有关的操作

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

替换 SortedList 中指定索引处的值。


SortedList sList = new SortedList();
sList.Add(1,"d");
sList.Add(2,"c");
sList.Add(3,"b");
sList.Add(4,"a");
sList.SetByIndex(1,"dddddd");   //1为索引,如果Count<2,则出错,也就是说必须存在
//而sList[2] = "dddddd";不存在这种现象,
//也就是说sList[2] = "dddddd"是
//如果键存在在修改值,不存在则添加
DropDownList3.DataSource = sList;
DropDownList3.DataTextField = "Key";
DropDownList3.DataValueField = "Value";
DropDownList3.DataBind();

2。public virtual object GetByIndex(int index);

获取 SortedList 的指定索引处的值。index必须小于Count,否则出错


SortedList sList = new SortedList();
sList.Add(1,"d");
sList.Add(2,"c");
sList.Add(3,"b");
sList.Add(4,"a");
//sList.Clear();
int nIndex = 2;
if (nIndex<sList.Count)
{
    Label3.Text = sList.GetByIndex(nIndex).ToString();
}
else
{
   Label3.Text = "nIndex>=Count";
}

3.public virtual int IndexOfKey(object key);

返回 SortedList 中指定键的从索引,这是Hashtable所没有的,因为Hashtable没有有序这个概念,它的排序是内部的

4.public virtual int IndexOfValue(object value);

返回指定的值在 SortedList 中第一个匹配项的索引,这是Hashtable所没有的,因为Hashtable没有有序这个概念,它的排序是内部的


SortedList sList = new SortedList();
sList.Add(1,"d");
sList.Add(2,"c");
sList.Add(3,"b");
sList.Add(4,"a");
sList.Add(5,"d");
int nIndex = 0;
nIndex = sList.IndexOfKey(1);   //为0
nIndex = sList.IndexOfValue("d"); //值匹配的有两个,这时返回第一个匹配的,所以为0

三。其他

1.public virtual object GetKey(int index);

获取 SortedList 的指定索引处的键,这也是Hashtable所不可能有的

2.public virtual IList GetKeyList();

获取 SortedList 中的键


SortedList sList = new SortedList();
sList.Add(1,"d");
sList.Add(2,"c");
sList.Add(3,"b");
sList.Add(4,"a");
sList.Add(5,"d");
Label3.Text = "";
IList iList = sList.GetKeyList();
for (int i=0; i<sList.Count; i++)
{
Label3.Text += iList[i].ToString();
Label3.Text += "   ";
}

注:IList 接口,表示可按照索引单独访问的一组对象,其中有一个Item属性,在C#也就就是索引器

3.public virtual IList GetValueList();

获取 SortedList 中的值

4.public virtual bool Contains(object key);

确定 SortedList 是否包含特定键

5.public virtual bool ContainsKey(object key);

确定 SortedList 是否包含特定键,与Contains(object key);完全同

6.public virtual bool ContainsValue(object value);

确定 SortedList 是否包含特定值

上述这三个函数与Hashtable完全相同

标签:c#,sortedlist
0
投稿

猜你喜欢

  • Android实现折线图小工具

    2023-08-03 10:17:22
  • C#遍历系统进程的方法

    2021-09-24 15:35:29
  • Java的Struts框架简介与环境配置教程

    2023-10-29 05:23:03
  • Android中的Intent Filter匹配规则简介

    2021-07-29 16:02:30
  • Java如何从json字符串中获取某个值详解

    2023-11-18 12:18:12
  • java nio基础使用示例

    2022-02-12 01:46:22
  • mybatis根据表逆向自动化生成代码的实现

    2023-03-12 15:39:02
  • WinForm拖拽控件生成副本的解决方法

    2022-07-02 13:28:26
  • Java Comparable及Comparator接口区别详解

    2023-07-03 08:19:50
  • 桌面浮动窗口(类似恶意广告)的实现详解

    2023-04-28 06:02:27
  • flutter传递值到任意widget(当需要widget嵌套使用需要传递值的时候)

    2022-05-17 22:53:44
  • C#使用ILGenerator动态生成函数的简单代码

    2022-07-10 01:41:06
  • C# 正则表达式进阶

    2022-12-26 01:56:32
  • springboot aspect通过@annotation进行拦截的实例代码详解

    2023-10-15 20:12:54
  • C#调用JS的几种方法

    2022-09-29 23:24:18
  • c#执行excel宏模版的方法

    2022-11-15 12:04:03
  • spring boot入门之诞生背景及优势影响

    2021-09-16 06:31:57
  • Android实现简单卡片布局

    2023-05-22 11:43:37
  • Java8新特性之lambda(动力节点Java学院整理)

    2022-01-16 21:35:54
  • springboot html调用js无效400问题及解决

    2023-06-24 02:11:54
  • asp之家 软件编程 m.aspxhome.com