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

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

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

實例:

Code
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

這個其實就是泛型結合數組的例子。

Code
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.Add("string");,obAppleArrayList.Add("heihei");,這樣obAppleArrayList的元素就不是單純的Apple類了。

我們最常用的也是List<T>.

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

實例:

Code
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();
         }
     }
}

 

Code
ArrayList obAppleArrayList = new ArrayList();
             obAppleArrayList.Add(objApple1);
             obAppleArrayList.Add(objApple2);
             obAppleArrayList.Add(objApple2);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章