學用 ASP.Net 之 System.Collections.SortedList 類

來源:互聯網
上載者:User
SortedList 是能自動排序的 "Key/Value" 列表類(排序是依據 Key), 並能通過索引訪問元素.

它像是 Hashtable(雜湊表)的升級, 它們的每個元素都是視為一個 DictionaryEntry(Key/Value) 結構體.

正因為它比 Hashtable 多出了排序和索引, 所以效率不及 Hashtable.

主要成員:
/* 屬性 */Capacity;        //容量Count;           //元素數Keys;            //鍵集合(ICollection)Values;          //值集合(ICollection)/* 方法 */Add()           //添加Clear()         //清空Contains()      //是否包含指定鍵ContainsKey()   //同 Contains()ContainsValue() //是否包含指定值GetByIndex()    //根據索引取 ValueGetKey()        //根據索引擷取 KeyGetKeyList()    //取鍵列表(IList)GetValueList()  //取值列表(IList)IndexOfKey()    //擷取指定鍵的索引IndexOfValue()  //擷取指定值的索引Remove()        //根據索引值刪除RemoveAt()      //根據索引刪除SetByIndex()    //根據索引設定值TrimToSize()    //最佳化容量(Capacity = Count)
元素會被自動排序:
protected void Button1_Click(object sender, EventArgs e){    SortedList sList = new SortedList();    sList.Add("2k", "x"); //值可以重複, 鍵不可以重複    sList.Add("1k", "xx");    sList.Add("4k", "xxx");    sList.Add("3k", "xxxx");    string str = "";    foreach (DictionaryEntry de in sList)    {        str += string.Format("{0} : {1}\n", de.Key, de.Value);    }    TextBox1.Text = str;}/********1k : xx2k : x3k : xxxx4k : xxx*********/
取值:
protected void Button1_Click(object sender, EventArgs e){    SortedList sList = new SortedList();    sList.Add("k1", "AAA");    sList.Add("k2", "BBB");    sList.Add("k3", "CCC");    sList.Add("k4", "DDD");    string s1 = sList["k2"].ToString();         //BBB    string s2 = sList.GetByIndex(1).ToString(); //BBB    string s3 = sList.GetKey(1).ToString();     //k2    TextBox1.Text = s1 + "\n" + s2 + "\n" + s3;}
改值:
protected void Button1_Click(object sender, EventArgs e){    SortedList sList = new SortedList();    sList.Add("4", "AAA");    sList.Add("3", "BBB");    sList.Add("2", "CCC");    sList.Add("1", "DDD");    sList["4"] = "aaa";    sList.SetByIndex(1, "ccc");    string str = "";    for (int i = 0; i 
刪除元素與擷取索引:
protected void Button1_Click(object sender, EventArgs e){    SortedList sList = new SortedList();    sList.Add("4", "AAA");    sList.Add("3", "BBB");    sList.Add("2", "CCC");    sList.Add("1", "DDD");    if (sList.Contains("2")) { sList.Remove("2"); }           // 將刪除 2/CCC    if (sList.Count > 0) { sList.RemoveAt(sList.Count - 1); } // 將刪除 4/AAA        //分別尋找剩餘元素的索引    int n1 = sList.IndexOfKey("3");     // 1    int n2 = sList.IndexOfKey("1");     // 0    int n3 = sList.IndexOfValue("BBB"); // 1    int n4 = sList.IndexOfValue("DDD"); // 0    sList.Clear();    int n5 = sList.IndexOfKey("3");     //-1 : 找不到則返回 -1    int n6 = sList.IndexOfValue("BBB"); //-1    TextBox1.Text = string.Concat(n1 + "\n" + n2 + "\n" + n3 + "\n" + n4 + "\n" + n5 + "\n" + n6);}
Capacity、Count 與 TrimToSize():
protected void Button1_Click(object sender, EventArgs e){    SortedList sList = new SortedList(100); //預留儲存 100 個元素的空間    int n1 = sList.Capacity; //100    int n2 = sList.Count;    //0    sList.Add(1, 111);    sList.Add(2, 222);    int n3 = sList.Capacity; //100    int n4 = sList.Count;    //2    sList.TrimToSize();    int n5 = sList.Capacity; //2    int n6 = sList.Count;    //2    TextBox1.Text = string.Concat(n1 + "\n" + n2 + "\n" + n3 + "\n" + n4 + "\n" + n5 + "\n" + n6);}
分別擷取鍵集合、值集合、鍵列表、值列表:
protected void Button1_Click(object sender, EventArgs e){    SortedList sList = new SortedList(5);    sList.Add("E", 5.55);    sList.Add("C", 3.33);    sList.Add("D", 4.44);    sList.Add("A", 1.11);    sList.Add("B", 2.22);    ICollection keys = sList.Keys;    ICollection values = sList.Values;    IList keyList = sList.GetKeyList();    IList valueList = sList.GetValueList();    string s1, s2, s3, s4;    s1 = s2 = s3 = s4 = "";    foreach (var k in keys) { s1 += k.ToString() + " "; }                              //A B C D E     foreach (var v in values) { s2 += v.ToString() + " "; }                            //1.11 2.22 3.33 4.44 5.55     for (int i = 0; i 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.