學用 ASP.Net 之 System.Collections.Generic 下的容器類

來源:互聯網
上載者:User
System.Collections.Generic.Dictionary;       //鍵/值對集合System.Collections.Generic.KeyValuePair;     //鍵/值對結構, 作為 Dictionary 的一個元素存在System.Collections.Generic.SortedDictionary; //相當於 Key 能自動排序 DictionarySystem.Collections.Generic.SortedList;       //和 SortedDictionary 功能相似, 但內部演算法不同, 其 Keys、Values 可通過索引訪問System.Collections.Generic.HashSet;   //無序、無重複的元素集合System.Collections.Generic.SortedSet; //相當於能自動排序的 HashSetSystem.Collections.Generic.List;      //相當於泛型的 ArrayList, 元素可重複、可排序、可插入、可索引訪問System.Collections.Generic.Queue; //隊列, 先進先出System.Collections.Generic.Stack; //堆棧, 後進先出System.Collections.Generic.LinkedList;     //雙向鏈表System.Collections.Generic.LinkedListNode; //LinkedList 的節點System.Collections.Generic.SynchronizedCollection;         //安全執行緒的集合System.Collections.Generic.SynchronizedReadOnlyCollection; //安全執行緒的唯讀集合System.Collections.Generic.SynchronizedKeyedCollection;    //安全執行緒的鍵/值集合
Dictionary、KeyValuePair:
protected void Button1_Click(object sender, EventArgs e){    Dictionary<string int> dict = new Dictionary<string int>();    dict.Add("K1", 123);    dict["K2"] = 456;    dict.Add("K3", 789);    string str = "";    foreach (KeyValuePair<string int> k in dict)    {        str += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-123; K2-456; K3-789;     }    TextBox1.Text = str;}</string></string></string>
SortedDictionary:
protected void Button1_Click(object sender, EventArgs e){    SortedDictionary<string int> dict = new SortedDictionary<string int>();    dict.Add("K3", 333);    dict["K1"] = 111;    dict.Add("K2", 222);    SortedDictionary<string int>.KeyCollection ks = dict.Keys;    SortedDictionary<string int>.ValueCollection vs = dict.Values;    string s1, s2, s3;    s1 = s2 = s3 = "";    foreach (KeyValuePair<string int> k in dict)    {        s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333;    }    foreach (string s in ks) { s2 += s + "; "; }          //K1; K2; K3;    foreach (int n in vs) { s3 += n.ToString() + "; "; }  //111; 222; 333;     TextBox1.Text = s1 + "\n" + s2 + "\n" + s3;}</string></string></string></string></string>
SortedList:
protected void Button1_Click(object sender, EventArgs e){    SortedList<string int> dict = new SortedList<string int>();    dict.Add("K3", 333);    dict["K1"] = 111;    dict.Add("K2", 222);    string s1, s2, s3;    s1 = s2 = s3 = "";    foreach (KeyValuePair<string int> k in dict)    {        s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333;    }    s2 = dict.Keys[0];              //K1    s3 = dict.Values[0].ToString(); //111    TextBox1.Text = s1 + "\n" + s2 + "\n" + s3;}</string></string></string>
HashSet、SortedSet:
protected void Button1_Click(object sender, EventArgs e){    HashSet<string> hs = new HashSet<string>();    hs.Add("ccc");    hs.Add("bbb");    hs.Add("aaa");    SortedSet<string> ss = new SortedSet<string>();    ss.Add("ccc");    ss.Add("bbb");    ss.Add("aaa");    string s1 = "", s2 = "";    foreach (string s in hs) { s1 += s + " "; } //ccc bbb aaa     foreach (string s in ss) { s2 += s + " "; } //aaa bbb ccc     TextBox1.Text = s1 + "\n" + s2;}</string></string></string></string>
List:
protected void Button1_Click(object sender, EventArgs e){    List<int> list = new List<int>();    list.Add(11);    list.Add(22);    list.Insert(0, 33);    string s1, s2 = "", s3, s4 = "";    s1 = list[0].ToString(); //33    for (int i = 0; i </int></int>
LinkedList、LinkedListNode:
protected void Button1_Click(object sender, EventArgs e){    LinkedList<string> list = new LinkedList<string>();    list.AddFirst("aaa");    list.AddLast("bbb");    list.AddFirst("ccc");    list.AddAfter(list.First, "ddd");    list.AddBefore(list.Last, "eee");    string s1 = "", s2 = "", s3 = "", s4 = "", s5 = "";    foreach (string s in list) { s1 += s + " "; } //ccc ddd aaa eee bbb     LinkedListNode<string> node = list.First;    s2 = node.Value.ToString();         //ccc    node = node.Next;    s3 = node.Value.ToString();         //ddd    node = list.Last.Previous.Previous;    s4 = node.Value.ToString();         //aaa    list.Remove("eee");    list.RemoveFirst();    list.RemoveLast();    node = list.First;    while (node != null)    {        s5 += node.Value.ToString() + " "; //ddd aaa         node = node.Next;    }    TextBox1.Text = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5;}</string></string></string>
相關文章

聯繫我們

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