C# 中ArrayList的用法

发布时间:2009年08月14日      浏览次数:880 次
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add(100);//单个添加
foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
{
al.Add(number);//集体添加方法一
}
int[] number2 = new int[2] { 11, 12 };
al.AddRange(number2);//集体添加方法二
al.Remove(3);//移除值为3的
al.RemoveAt(3);//移除第3个
ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份
Console.WriteLine("遍历方法一:");
foreach (int i in al)//不要强制转换
{
Console.WriteLine(i);//遍历方法一
}
Console.WriteLine("遍历方法二:");
for (int i = 0; i < al2.Count; i++)//数组是length
{
int number = (int)al2[i];//一定要强制转换
Console.WriteLine(number);//遍历方法二
}
}
}
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!