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

来源:互联网 发布:淘宝店铺差评怎么删除 编辑:程序博客网 时间:2024/06/01 08:46
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 oin 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 oin objAppleList)
             {
                 Console.WriteLine(
"Color is {0},Weight is {1}", o.Color, o.Weight);
             }

            
//总的个数:
             Console.WriteLine("objAppleList的总个数是:{0}", objAppleList.Count);

             Console.ReadLine();
         }
     }

    
public class Apple
     {
        
//定义字段
        privatestring _color= "";
        
privatedecimal _weight= 0;

        
//定义跟字段对应的属性
        publicstring Color
         {
            
get {return _color; }
            
set { _color= value; } //这里的value是C#关键字。表示外面传入的值.
         }

        
publicdecimal 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 keyin keys)
             {
                 Console.WriteLine(
"Key is {0},Values is {1}",key,objHashtable[key]);
             }

             Console.WriteLine();

            
//换一种遍历方式:
            foreach (DictionaryEntry dein 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);
原创粉丝点击