/*
实验ArrayList类的方法。
*/
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
ArrayList myAL = new ArrayList(); //ArrayList动态数组
myAL.Capacity = 6; //ArrayList可包含的元素数,当容量超过6时,其容量自动增加一倍
for (int i = 0; i < 10; i++)
{
myAL.Add(i); //向数组中添加元素
}
Console.Write("数组为:");
foreach (int item in myAL)
{
Console.Write("{0}", item.ToString());
}
Console.WriteLine();
Console.Write("删除元素后的数组为:");
myAL.RemoveAt(2); //删除指定位置的元素
foreach (int item in myAL)
{
Console.Write("{0}", item.ToString());
}
Console.WriteLine();
myAL.Insert(7, 5); //向指定位置插入元素
Console.Write("插入元素后的数组为:");
foreach (int item in myAL)
{
Console.Write("{0}", item.ToString());
}
Console.WriteLine();
Console.Write("数组反序后为:");
myAL.Reverse(); //将数组反序
foreach (int item in myAL)
Console.Write("{0}", item.ToString());
Console.WriteLine();
Console.WriteLine("是否包含指定元素5?{0}", myAL.Contains(5)); //是否包含指定元素
Console.WriteLine("元素3的位置为:{0}", myAL.IndexOf(3) + 1);
Console.WriteLine("元素个数:{0}", myAL.Count); //Count:数组中实际包含的个数
Console.WriteLine("动态数组|容量:{0}", myAL.Capacity); //Capacity:数组的容量
object[] array = myAL.ToArray(); //ToArray()将ArrayList元素复制到指定数组中,返回object型
Console.Write("复制后的数组为:");
foreach (int item in array)
{
Console.Write("{0}", item);
}
Console.ReadKey();
}
}
}
运行结果: