zoukankan
html css js c++ java
SortedList 用法
表示键
/
值对的集合,这些键和值按键排序并可按照键和索引访问。
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完全相同
查看全文
相关阅读:
合并区间
编程团体赛
寻找数组的中间位置
翻转链表2
链表翻转
CF1237H. Balanced Reversals
arc108E
agc028D
CF1446D. Frequency Problem
CF1439D. INOI Final Contests
原文地址:https://www.cnblogs.com/kokoliu/p/524417.html
最新文章
今天学到了一个快速正则匹配的小tips~
判断某个字符串b以某列表a中任意元素开头
正则表达式匹配某几个字前面的字符并且删除
每日一题力扣450
合并两个字典:**
每日一题力扣669 修剪二叉搜索树
判断类型isinstance
yield
jenkins执行Build Now报错系列(一)不能执行循环赋值
jenkins执行Build Now报错系列(一)Failed to change to remote director
热门文章
jenkins执行Build Now报错系列(一)共享冲突
jenkins执行Build Now报错系列(一)找不到文件
jenkins执行Build Now报错系列(一)Building for production... ERROR Failed to compile
jenkins执行Build Now报错系列(一)'vue-cli-service' 不是内部或外部命令,也不是可运行的程序
【2021-06-25】路都是从无到有走出来的
【2021-06-24】期待着自己能坐地铁上下班
最长公共前缀
零矩阵
求质因数
旋转矩阵
Copyright © 2011-2022 走看看