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