C# 集合

集合的定義網上應該就可以找到很多,所以我列出明確的分別就好

List:固定的型別,不固定長度
Array:任意的型別,固定長度
ArrayList:任意的型別,任意的長度
HashSet:裡面的值不會重複
Queue:先進先出,從屁屁塞進序列
Stack:後進先出,從頭塞進序列
Dictionary:一個key 對應 一個value

除了Array不寫程式碼,以下是各自的程式碼及結果


ArrayList:

private void button1_Click(object sender, EventArgs e)
{
    //物件版集合
    ArrayList al = new ArrayList();
    al.Add(100);
    al.Add("哭笑不得");
    al.Add(50.7);
    al.Add(new Button());
    al.Add("C#.NET");
    al.Add(100);
    al.Add(80.6);

    StringBuilder sb = new StringBuilder();
    double totalSum = 0;
    for (int i = 0; i < al.Count; i++)
    {
        //sb.Append(al[i].ToString()).Append("\r\n");
        /*
        int a;
        double b;
        if (int.TryParse(al[i].ToString(), out a))
            totalSum += a;
        else if(double.TryParse(al[i].ToString(), out b))
            totalSum += b;
        */
        /*
        if (al[i].GetType() == Type.GetType("System.Int32"))
            totalSum += (int)al[i];
        else if (al[i].GetType() == Type.GetType("System.Double"))
            totalSum += (double)al[i];
        */
        if (al[i] is int)
            totalSum += (int)al[i];
        else if (al[i] is double)
            totalSum += (double)al[i];
    }

    textBox1.Text = totalSum.ToString();
            
}

result:331.3

List:

private void button2_Click(object sender, EventArgs e)
{
    //泛型版集合,不是double 的放不進去
    List al = new List();
    al.Add(100);
    //al.Add("哭笑不得");
    al.Add(50.7);
    //al.Add(new Button());
    //al.Add("C#.NET");
    al.Add(100);
    al.Add(80.6);

    double totalSum=0;
    for (int i = 0; i < al.Count; i++)
        totalSum += al[i];
    textBox1.Text = totalSum.ToString();

} 

result:331.3

HashSet:

private void button3_Click(object sender, EventArgs e)
{
    HashSet hs = new HashSet();
    hs.Add(100);
    hs.Add(400);
    hs.Add(300);
    hs.Add(200);
    hs.Add(500);
    hs.Add(200);
    hs.Add(100);
    hs.Add(500);
            
    StringBuilder sb = new StringBuilder();
    foreach (int a in hs)
        sb.Append(a).Append("\r\n");

    textBox1.Text = sb.ToString();
} 

result: 
100
400
300
200
500

Queue:

private void button4_Click(object sender, EventArgs e)
{
    Queue qq = new Queue();
    //先進先出從屁屁塞
    qq.Enqueue(100);
    qq.Enqueue(200);
    qq.Enqueue(300);
    qq.Enqueue(400);
    qq.Enqueue(500);

    StringBuilder sb = new StringBuilder();
    /*
    foreach (int a in qq)
        sb.Append(String.Format("{0}\t{1}\r\n", a,qq.Count));
    */
    //逐一取出        
    while (qq.Count>0)
        sb.Append(String.Format("{0}\t{1}\r\n", qq.Dequeue(),qq.Count));
            
    textBox1.Text = sb.ToString();
}

result:
100 4
200 3
300 2
400 1
500 0


Stack:

private void button5_Click(object sender, EventArgs e)
{
    Stack st = new Stack();
    //後進先出從頭塞
    st.Push(100);
    st.Push(200);
    st.Push(300);
    st.Push(400);
    st.Push(500);

    StringBuilder sb = new StringBuilder();
    /*
    foreach (int a in qq)
        sb.Append(String.Format("{0}\t{1}\r\n", a,qq.Count));
    */

    while (st.Count > 0)
        sb.Append(String.Format("{0}\t{1}\r\n", st.Pop(), st.Count));

    textBox1.Text = sb.ToString();
}

result
500 4
400 3
300 2
200 1
100 0

Dictionary:

private void button6_Click(object sender, EventArgs e)
{
    Dictionary dc = new Dictionary();
    dc.Add("王曉明", 80);
    dc.Add("李小英", 75);
    dc.Add("林大雄", 65);
    dc.Add("孫小美", 55);
    dc.Add("大老千", 43);
    dc.Add("忍太郎", 65);

    if (dc.ContainsKey("李小英"))
        dc["李小英"] = 70;
    else
        dc.Add("李小英", 70);

    if (dc.ContainsKey("孫小美"))
        dc["孫小美"] = 96;
    else
        dc.Add("孫小美", 96);

    //textBox1.Text = dc["孫小美"].ToString();

    StringBuilder sb = new StringBuilder();
    foreach (KeyValuePair kv in dc)
        sb.Append(String.Format("{0}:{1}\r\n", kv.Key, kv.Value));
    textBox1.Text = sb.ToString();

}/pre>

result
王曉明:80
李小英:70
林大雄:65
孫小美:96
大老千:43
忍太郎:65

留言

熱門文章