欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#的sortedlist使用方法

 更新時(shí)間:2014年05月01日 08:56:00   作者:  
這篇文章主要介紹了c#的sortedlist使用方法,需要的朋友可以參考下

表示鍵/值對(duì)的集合,這些鍵和值按鍵排序并可按照鍵和索引訪問。

SortedList最合適對(duì)一列健/值對(duì) 進(jìn)行排序,在排序時(shí),是對(duì)鍵進(jìn)行排序,SortedList 是 Hashtable 和 Array 的混合。當(dāng)使用 Item 索引器屬性按照元素的鍵訪問元素時(shí),其行為類似于 Hashtable。當(dāng)使用 GetByIndex 或 SetByIndex 按照元素的索引訪問元素時(shí),其行為類似于 Array。

SortedList 在內(nèi)部維護(hù)兩個(gè)數(shù)組以將數(shù)組存儲(chǔ)到列表中;即,一個(gè)數(shù)組用于鍵,另一個(gè)數(shù)組用于相關(guān)聯(lián)的值。每個(gè)元素都是一個(gè)可作為 DictionaryEntry 對(duì)象進(jìn)行訪問的鍵/值對(duì)。鍵不能為空引用(Visual Basic 中為 Nothing),但值可以。SortedList 的容量是列表可擁有的元素?cái)?shù)。隨著向 SortedList 中添加元素,容量通過重新分配按需自動(dòng)增加??赏ㄟ^調(diào)用 TrimToSize 或通過顯式設(shè)置 Capacity 屬性減少容量。SortedList 的元素將按照特定的 IComparer 實(shí)現(xiàn)(在創(chuàng)建SortedList 時(shí)指定)或按照鍵本身提供的 IComparable 實(shí)現(xiàn)并依據(jù)鍵來進(jìn)行排序。不論在哪種情況下,SortedList 都不允許重復(fù)鍵。

索引順序基于排序順序。當(dāng)添加元素時(shí),元素將按正確的排序順序插入 SortedList,同時(shí)索引會(huì)相應(yīng)地進(jìn)行調(diào)整。若移除了元素,索引也會(huì)相應(yīng)地進(jìn)行調(diào)整。因此,當(dāng)在SortedList 中添加或移除元素時(shí),特定鍵/值對(duì)的索引可能會(huì)更改。

由于要進(jìn)行排序,所以在 SortedList 上操作比在 Hashtable 上操作要慢。但是,SortedList 允許通過相關(guān)聯(lián)鍵或通過索引對(duì)值進(jìn)行訪問,可提供更大的靈活性。

一。添加刪除

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

此集合中的索引從零開始。

將帶有指定鍵和值的元素添加到 SortedList。

通過設(shè)置 SortedList 中不存在的鍵的值,Item 屬性也可用于添加新元素。例如:myCollection["myNonexistentKey"] = myValue。但是,如果指定的鍵已經(jīng)存在于 SortedList 中,則設(shè)置 Item 屬性將改寫舊值。相比之下,Add 方法不修改現(xiàn)有元素。

復(fù)制代碼 代碼如下:

SortedList sList = new SortedList();
sList.Add(1,"d");
sList.Add(2,"c");
sList.Add(3,"b");
sList.Add(4,"a");
//結(jié)果為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 保持不變。不引發(fā)異常

復(fù)制代碼 代碼如下:

SortedList sList = new SortedList();
sList.Add(1,"d");
sList.Add(2,"c");
sList.Add(3,"b");
sList.Add(4,"a");
//sList.Remove("b");   錯(cuò)誤,是按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 的指定索引處的元素。

復(fù)制代碼 代碼如下:

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"],這里的參數(shù)是索引號(hào),而非鍵值,
//與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 設(shè)置為零。Capacity 保持不變。若要重置 SortedList 的容量,請(qǐng)調(diào)用 TrimToSize或直接設(shè)置 Capacity 屬性。截去空 SortedList 會(huì)將 SortedList 的容量設(shè)置為默認(rèn)容量,而不是零

二。與索引有關(guān)的操作

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

替換 SortedList 中指定索引處的值。

復(fù)制代碼 代碼如下:

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,則出錯(cuò),也就是說必須存在
//而sList[2] = "dddddd";不存在這種現(xiàn)象,
//也就是說sList[2] = "dddddd"是
//如果鍵存在在修改值,不存在則添加
DropDownList3.DataSource = sList;
DropDownList3.DataTextField = "Key";
DropDownList3.DataValueField = "Value";
DropDownList3.DataBind();

2。public virtual object GetByIndex(int index);

獲取 SortedList 的指定索引處的值。index必須小于Count,否則出錯(cuò)

復(fù)制代碼 代碼如下:

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所沒有的,因?yàn)镠ashtable沒有有序這個(gè)概念,它的排序是內(nèi)部的

4.public virtual int IndexOfValue(object value);

返回指定的值在 SortedList 中第一個(gè)匹配項(xiàng)的索引,這是Hashtable所沒有的,因?yàn)镠ashtable沒有有序這個(gè)概念,它的排序是內(nèi)部的

復(fù)制代碼 代碼如下:

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"); //值匹配的有兩個(gè),這時(shí)返回第一個(gè)匹配的,所以為0

三。其他

1.public virtual object GetKey(int index);

獲取 SortedList 的指定索引處的鍵,這也是Hashtable所不可能有的

2.public virtual IList GetKeyList();

獲取 SortedList 中的鍵

復(fù)制代碼 代碼如下:

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 接口,表示可按照索引單獨(dú)訪問的一組對(duì)象,其中有一個(gè)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 是否包含特定值

上述這三個(gè)函數(shù)與Hashtable完全相同

相關(guān)文章

最新評(píng)論