现在的位置: 首页 > 综合 > 正文

SortedList 与ArrayList

2012年11月09日 ⁄ 综合 ⁄ 共 4202字 ⁄ 字号 评论关闭

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

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完全相同

 

 

7.public virtual void TrimToSize();

将容量设置为 SortedList 中元素的实际数目

抱歉!评论已关闭.