c# 集合類:ArrayList,StringCollection,Hashtable,List

來源:互聯網
上載者:User
1.數組集合
其實,在數組的一節裡面已經包含了這個概念了。其實數組集合就是 new int[2];
官方參考地址:http://msdn.microsoft.com/zh-cn/library/57yac89c(VS.80).aspx
2.ArrayList
ArrayList跟數組(Array)的區別:http://msdn.microsoft.com/zh-cn/library/41107z8a(VS.80).aspx
執行個體:

using ...System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace CSharp
...{
    public class TestArrayList
    ...{
        public TestArrayList()
        ...{
            // Create an empty ArrayList, and add some elements.
            ArrayList stringList = new ArrayList();

            stringList.Add("a");
            stringList.Add("abc");
            stringList.Add("abcdef");
            stringList.Add("abcdefg");
            stringList.Add(20);

            // 索引或者說數組下標是數字,所以不需要名字.
            Console.WriteLine("Element ...{0} is \"{1}\"", 2, stringList[2]);

            // 給下標為2的元素賦值
            stringList[2] = "abcd";
            Console.WriteLine("Element ...{0} is \"{1}\"", 2, stringList[2]);

            // 輸出stringList的總的元素個素
            Console.WriteLine("Number of elements in the list: ...{0}",
                stringList.Count);

            try
            ...{
                //數組下標從0到count-1,如果嘗試輸出小於0或者大於等於count的下標,將拋出異常。
                Console.WriteLine("Element ...{0} is \"{1}\"",
                    stringList.Count, stringList[stringList.Count]);
            }
            catch (ArgumentOutOfRangeException aoore)
            ...{
                Console.WriteLine("stringList(...{0}) is out of range(越界).",
                    stringList.Count);
            }

            // 不能使用這種方式來增加元素,只能通過stringList.add("aa")來增加元素
            try
            ...{
                stringList[stringList.Count] = "42";
            }
            catch (ArgumentOutOfRangeException aoore)
            ...{
                Console.WriteLine("stringList(...{0}) is out of range(越界).",
                    stringList.Count);
            }

            Console.WriteLine();
            //用for來迴圈
            for (int i = 0; i < stringList.Count; i++)
            ...{
                Console.WriteLine("Element ...{0} is \"{1}\"", i,
                    stringList[i]);
            }

            Console.WriteLine();
            //用foreach來迴圈
            foreach (object o in stringList)
            ...{
                Console.WriteLine(o);
            }

            Console.ReadLine();
        }
    }
}

這裡同時要提到StringCollection,其實這個跟ArrayList沒啥區別,只不過StringCollection只能接收字元類型的東西。
官方地址:http://msdn.microsoft.com/zh-cn/library/system.collections.specialized.stringcollection(VS.80).aspx

3.List<T> ,這個我想才是我們最常用的。
官方參考地址:http://msdn.microsoft.com/zh-cn/library/6sh2ey19(VS.80).aspx
這個其實就是泛型結合數組的例子。

using ...System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharp
...{
    public class TestList
    ...{
        //預設建構函式
        public TestList()
        ...{
            //聲明文法,換句話說就是:定義objAppleList - 集合變數的文法。
            List<Apple> objAppleList = new List<Apple>();

            //定義3個Apple類的執行個體(也叫對象)
            Apple objApple1 = new Apple();
            objApple1.Color = "red";
            objApple1.Weight = 10;

            Apple objApple2 = new Apple();
            objApple2.Color = "green";
            objApple2.Weight = 12;

            Apple objApple3 = new Apple();
            objApple3.Color = "black";
            objApple3.Weight = 8;

            //把3個Apple類的執行個體 幹到 objAppleList裡面去。

            objAppleList.Add(objApple1);
            objAppleList.Add(objApple2);
            objAppleList.Add(objApple3);

            //遍曆objAppleList這個集合.
            foreach (Apple o in objAppleList)
            ...{
                Console.WriteLine("Color is ...{0},Weight is {1}", o.Color, o.Weight);
            }

            //總的個數:
            Console.WriteLine("objAppleList的總個數是:...{0}", objAppleList.Count);

            Console.ReadLine();
        }
    }

    public class Apple
    ...{
        //定義欄位
        private string _color = "";
        private decimal _weight = 0;

        //定義跟欄位對應的屬性
        public string Color
        ...{
            get ...{ return _color; }
            set ...{ _color = value; } //這裡的value是C#關鍵字。表示外面傳入的值.
        }

        public decimal Weight
        ...{
            get ...{ return _weight; }
            set ...{ _weight = value; }
        }
    }
}

在這裡:List<Apple> objAppleList = new List<Apple>();,其實我們用數組也可以,如:Apple[] objAappArray = new Apple[3]; 但是數組的限制就是固定了大小。不能動態增加。
這裡為什麼不用ArrayList? 按道理,用ArrayList也可以,如:

ArrayList obAppleArrayList = new ArrayList();
            obAppleArrayList.Add(objApple1);
            obAppleArrayList.Add(objApple2);
            obAppleArrayList.Add(objApple2);

我們不用的ArrayList的目的是保證型別安全。因為這個時候,你還可以obAppleArrayList.Add("string");,obAppleArrayList.Add("heihei");,這樣obAppleArrayList的元素就不是單純的Apple類了。
我們最常用的也是List<T>.

4.Hashtable,
官方地址:http://msdn.microsoft.com/zh-cn/library/system.collections.hashtable(VS.80).aspx
執行個體:

using ...System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace CSharp
...{
    public class TestHashtable
    ...{
        public TestHashtable()
        ...{
            Hashtable objHashtable = new Hashtable();
            
            //需要注意的是:這裡的add有點不同於ArrayList,這裡需要指定兩個值,一個是key,一個value.
            //而且必須都是Object
            objHashtable.Add("Key", "Value");
            objHashtable.Add(1, 2);
            objHashtable.Add(2.1, 3.2);

            //擷取所有的key
            ICollection keys = objHashtable.Keys;
            foreach (object key in keys)
            ...{
                Console.WriteLine("Key is ...{0},Values is {1}",key,objHashtable[key]);
            }

            Console.WriteLine();

            //換一種遍曆方式:
            foreach (DictionaryEntry de in objHashtable )
            ...{
                Console.WriteLine("Key is ...{0},Values is {1}", de.Key, de.Value);
            }

            Console.ReadLine();
        }
    }
}

 

聯繫我們

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