学用 ASP.Net 之 System.Collections.SortedList 类


SortedList 是能自动排序的 "Key/Value" 列表类(排序是依据 Key), 并能通过索引访问元素.

它像是 Hashtable(哈希表)的升级, 它们的每个元素都是视为一个 DictionaryEntry(Key/Value) 结构体.

正因为它比 Hashtable 多出了排序和索引, 所以效率不及 Hashtable.
主要成员:
/* 属性 */
Capacity;        //容量
Count;           //元素数
Keys;            //键集合(ICollection)
Values;          //值集合(ICollection)

/* 方法 */
Add()           //添加
Clear()         //清空
Contains()      //是否包含指定键
ContainsKey()   //同 Contains()
ContainsValue() //是否包含指定值
GetByIndex()    //根据索引取 Value
GetKey()        //根据索引获取 Key
GetKeyList()    //取键列表(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 : xx
2k : x
3k : xxxx
4k : 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 < sList.Count; i++)
    {
        str += sList.GetByIndex(i).ToString() + " "; //DDD ccc BBB aaa 
    }
    TextBox1.Text = str;
}

删除元素与获取索引:
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 < keyList.Count; i++) { s3 += keyList[i].ToString() + " "; }     //A B C D E 
    for (int i = 0; i < valueList.Count; i++) { s4 += valueList[i].ToString() + " "; } //1.11 2.22 3.33 4.44 5.55 

    TextBox1.Text = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4;
}

你可能感兴趣的:(学用 ASP.Net 之 System.Collections.SortedList 类)