国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#的sortedlist使用方法

2020-01-24 02:41:18
字體:
來源:轉載
供稿:網(wǎng)友

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

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

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

索引順序基于排序順序。當添加元素時,元素將按正確的排序順序插入 SortedList,同時索引會相應地進行調整。若移除了元素,索引也會相應地進行調整。因此,當在SortedList 中添加或移除元素時,特定鍵/值對的索引可能會更改。

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

一。添加刪除

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

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

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

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

復制代碼 代碼如下:

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 保持不變。不引發(fā)異常

復制代碼 代碼如下:

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"],這里的參數(shù)是索引號,而非鍵值,
//與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";不存在這種現(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,否則出錯

復制代碼 代碼如下:

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沒有有序這個概念,它的排序是內(nèi)部的

4.public virtual int IndexOfValue(object value);

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

復制代碼 代碼如下:

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 是否包含特定值

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 建阳市| 鹤山市| 东平县| 泾川县| 长子县| 赤城县| 蕲春县| 闻喜县| 类乌齐县| 泽普县| 香港| 汉沽区| 枝江市| 乌兰浩特市| 遂川县| 福清市| 新平| 连平县| 曲阜市| 五河县| 武义县| 抚远县| 会理县| 滦南县| 四会市| 湾仔区| 新闻| 庄河市| 福建省| 平乡县| 手机| 蓬溪县| 远安县| 固安县| 商城县| 广平县| 侯马市| 临海市| 望都县| 靖远县| 门头沟区|