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

实例:

ContractedBlock.gif ExpandedBlockStart.gif 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

这个其实就是泛型结合数组的例子。

ContractedBlock.gif ExpandedBlockStart.gif 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也可以,如:

ContractedBlock.gif ExpandedBlockStart.gif Code
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

实例:

ContractedBlock.gif ExpandedBlockStart.gif 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(
12);
            objHashtable.Add(
2.13.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();
        }
    }
}

转载于:https://www.cnblogs.com/nevernet/archive/2009/01/08/1371856.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值