SortedList:asp.net入門筆記(七)

來源:互聯網
上載者:User

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

7.public virtual void TrimToSize();

將容量設定為 SortedList 中元素的實際數目

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.