Code
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);//遍历方法二
}
}
}
}
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);//遍历方法二
}
}
}
}