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

本文详细介绍了C#中数组集合、ArrayList、StringCollection、List<T>及Hashtable等集合类的使用方法与特点,并通过示例代码展示了每种集合的具体操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace CSharp
{
public class TestArrayList
{
public TestArrayList()
{
// CreateanemptyArrayList,andaddsomeelements.
ArrayListstringList = 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( " Numberofelementsinthelist:{0} " ,
stringList.Count);

try
{
// 数组下标从0到count-1,如果尝试输出小于0或者大于等于count的下标,将抛出异常。
Console.WriteLine( " Element{0}is\ " { 1 }\ "" ,
stringList.Count,stringList[stringList.Count]);
}
catch (ArgumentOutOfRangeExceptionaoore)
{
Console.WriteLine(
" stringList({0})isoutofrange(越界). " ,
stringList.Count);
}

// 不能使用这种方式来增加元素,只能通过stringList.add("aa")来增加元素
try
{
stringList[stringList.Count]
= " 42 " ;
}
catch (ArgumentOutOfRangeExceptionaoore)
{
Console.WriteLine(
" stringList({0})isoutofrange(越界). " ,
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
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->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类的实例(也叫对象)
AppleobjApple1 = new Apple();
objApple1.Color
= " red " ;
objApple1.Weight
= 10 ;

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

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

// 把3个Apple类的实例干到objAppleList里面去。

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

// 遍历objAppleList这个集合.
foreach (Appleo in objAppleList)
{
Console.WriteLine(
" Coloris{0},Weightis{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也可以,如:

Code
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->ArrayListobAppleArrayList = 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

实例:

Code
<!-- <br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace CSharp
{
public class TestHashtable
{
public TestHashtable()
{
HashtableobjHashtable
= new Hashtable();

// 需要注意的是:这里的add有点不同于ArrayList,这里需要指定两个值,一个是key,一个value.
// 而且必须都是Object
objHashtable.Add( " Key " , " Value " );
objHashtable.Add(
1 , 2 );
objHashtable.Add(
2.1 , 3.2 );

// 获取所有的key
ICollectionkeys = objHashtable.Keys;
foreach ( object key in keys)
{
Console.WriteLine(
" Keyis{0},Valuesis{1} " ,key,objHashtable[key]);
}

Console.WriteLine();

// 换一种遍历方式:
foreach (DictionaryEntryde in objHashtable)
{
Console.WriteLine(
" Keyis{0},Valuesis{1} " ,de.Key,de.Value);
}

Console.ReadLine();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值