zoukankan      html  css  js  c++  java
  • System.Collections.Generic的各容器类的用法

    演示System.Collections.Generic的各容器类的用法.

    包括:Dictionary,KeyValuePair,SortedDic tionary,SortedList,HashSet,SortedSet,List,Queue,Stack等

      1 System.Collections.Generic.Dictionary<>;       //键/值对集合
      2 System.Collections.Generic.KeyValuePair<>;     //键/值对结构, 作为 Dictionary<> 的一个元素存在
      3 System.Collections.Generic.SortedDictionary<>; //相当于 Key 能自动排序 Dictionary<>
      4 System.Collections.Generic.SortedList<>;       //和 SortedDictionary<> 功能相似, 但内部算法不同, 其 Keys、Values 可通过索引访问
      5 
      6 System.Collections.Generic.HashSet<>;   //无序、无重复的元素集合
      7 System.Collections.Generic.SortedSet<>; //相当于能自动排序的 HashSet<>
      8 System.Collections.Generic.List<>;      //相当于泛型的 ArrayList, 元素可重复、可排序、可插入、可索引访问
      9 
     10 System.Collections.Generic.Queue<>; //队列, 先进先出
     11 System.Collections.Generic.Stack<>; //堆栈, 后进先出
     12 
     13 System.Collections.Generic.LinkedList<>;     //双向链表
     14 System.Collections.Generic.LinkedListNode<>; //LinkedList<> 的节点
     15 
     16 System.Collections.Generic.SynchronizedCollection<>;         //线程安全的集合
     17 System.Collections.Generic.SynchronizedReadOnlyCollection<>; //线程安全的只读集合
     18 System.Collections.Generic.SynchronizedKeyedCollection<>;    //线程安全的键/值集合
     19 
     20 Dictionary<>、KeyValuePair<>:
     21 protected void Button1_Click(object sender, EventArgs e)
     22 {
     23     Dictionary<string, int> dict = new Dictionary<string, int>();
     24     dict.Add("K1", 123);
     25     dict["K2"] = 456;
     26     dict.Add("K3", 789);
     27 
     28     string str = "";
     29     foreach (KeyValuePair<string, int> k in dict)
     30     {
     31         str += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-123; K2-456; K3-789; 
     32     }
     33     TextBox1.Text = str;
     34 }
     35 
     36 SortedDictionary<>:
     37 protected void Button1_Click(object sender, EventArgs e)
     38 {
     39     SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
     40     dict.Add("K3", 333);
     41     dict["K1"] = 111;
     42     dict.Add("K2", 222);
     43 
     44     SortedDictionary<string, int>.KeyCollection ks = dict.Keys;
     45     SortedDictionary<string, int>.ValueCollection vs = dict.Values;
     46 
     47     string s1, s2, s3;
     48     s1 = s2 = s3 = "";
     49 
     50     foreach (KeyValuePair<string, int> k in dict)
     51     {
     52         s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333;
     53     }
     54 
     55     foreach (string s in ks) { s2 += s + "; "; }          //K1; K2; K3;
     56     foreach (int n in vs) { s3 += n.ToString() + "; "; }  //111; 222; 333; 
     57 
     58     TextBox1.Text = s1 + "\n" + s2 + "\n" + s3;
     59 }
     60 
     61 SortedList<>:
     62 protected void Button1_Click(object sender, EventArgs e)
     63 {
     64     SortedList<string, int> dict = new SortedList<string, int>();
     65     dict.Add("K3", 333);
     66     dict["K1"] = 111;
     67     dict.Add("K2", 222);
     68 
     69     string s1, s2, s3;
     70     s1 = s2 = s3 = "";
     71 
     72     foreach (KeyValuePair<string, int> k in dict)
     73     {
     74         s1 += string.Format("{0}-{1}; ", k.Key, k.Value); //K1-111; K2-222; K3-333;
     75     }
     76 
     77     s2 = dict.Keys[0];              //K1
     78     s3 = dict.Values[0].ToString(); //111
     79 
     80     TextBox1.Text = s1 + "\n" + s2 + "\n" + s3;
     81 }
     82 
     83 HashSet<>、SortedSet<>:
     84 protected void Button1_Click(object sender, EventArgs e)
     85 {
     86     HashSet<string> hs = new HashSet<string>();
     87     hs.Add("ccc");
     88     hs.Add("bbb");
     89     hs.Add("aaa");
     90 
     91     SortedSet<string> ss = new SortedSet<string>();
     92     ss.Add("ccc");
     93     ss.Add("bbb");
     94     ss.Add("aaa");
     95 
     96     string s1 = "", s2 = "";
     97 
     98     foreach (string s in hs) { s1 += s + " "; } //ccc bbb aaa 
     99     foreach (string s in ss) { s2 += s + " "; } //aaa bbb ccc 
    100 
    101     TextBox1.Text = s1 + "\n" + s2;
    102 }
    103 
    104 List<>:
    105 protected void Button1_Click(object sender, EventArgs e)
    106 {
    107     List<int> list = new List<int>();
    108     list.Add(11);
    109     list.Add(22);
    110     list.Insert(0, 33);
    111 
    112     string s1, s2 = "", s3, s4 = "";
    113 
    114     s1 = list[0].ToString(); //33
    115     for (int i = 0; i < list.Count; i++) { s2 += list[i].ToString() + " "; } //33 11 22
    116 
    117     list.Sort();
    118 
    119     s3 = list[0].ToString(); //11
    120     foreach (int n in list) { s4 += n.ToString() + " "; } //11 22 33 
    121 
    122     TextBox1.Text = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4;
    123 }
    124 
    125 LinkedList<>、LinkedListNode<>:
    126 protected void Button1_Click(object sender, EventArgs e)
    127 {
    128     LinkedList<string> list = new LinkedList<string>();
    129     list.AddFirst("aaa");
    130     list.AddLast("bbb");
    131     list.AddFirst("ccc");
    132     list.AddAfter(list.First, "ddd");
    133     list.AddBefore(list.Last, "eee");
    134 
    135     string s1 = "", s2 = "", s3 = "", s4 = "", s5 = "";
    136 
    137     foreach (string s in list) { s1 += s + " "; } //ccc ddd aaa eee bbb 
    138 
    139     LinkedListNode<string> node = list.First;
    140     s2 = node.Value.ToString();         //ccc
    141     node = node.Next;
    142     s3 = node.Value.ToString();         //ddd
    143     node = list.Last.Previous.Previous;
    144     s4 = node.Value.ToString();         //aaa
    145 
    146     list.Remove("eee");
    147     list.RemoveFirst();
    148     list.RemoveLast();
    149 
    150     node = list.First;
    151     while (node != null)
    152     {
    153         s5 += node.Value.ToString() + " "; //ddd aaa 
    154         node = node.Next;
    155     }
    156     TextBox1.Text = s1 + "\n" + s2 + "\n" + s3 + "\n" + s4 + "\n" + s5;
    157 }
    
     
  • 相关阅读:
    EF使用CodeFirst创建数据库和表
    EF使用CodeFirst创建数据库和表
    MVC实现实现文件流打包成压缩包
    MVC实现实现文件流打包成压缩包
    MVC实现实现文件流打包成压缩包
    Visual Studio Code 开发 .NET Core 看这篇就够了
    Visual Studio Code 开发 .NET Core 看这篇就够了
    P1438 无聊的数列
    P3380 【模板】二逼平衡树(树套树)
    SP16580 QTREE7
  • 原文地址:https://www.cnblogs.com/JerryTian/p/5148884.html
Copyright © 2011-2022 走看看