[作业10-11]2.ArrayList的用法

Posted on 2009-10-15 14:56  Relax Active  阅读(190)  评论(0)    收藏  举报

说明:数组列表用法类似于数组,但数组列表可以增大,它由 System.Collections.ArrayList来表示。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace ArrayList3
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList city = new ArrayList();     //实例化一个数组列表!
            city.Add("Beijing");                    //往列表添加一个字符串!
            city.Add("Shenzhen");
            city.Add("Guangzhou");

            string[] str = new string[2];      //实例化一个字符串类型数组!      
            str[0] = "Shanghai";                //为数组赋值!
            str[1] = "Hongkong";

            city.AddRange(str);              //用AddRange方法把数组值的传给列表city!

            city.RemoveRange(2,2);             //用RemoveRange方法删除列表值!

            foreach (string item in city)      //foreach循环枚举列表city值!
            {
                Console.WriteLine(item);          //输出变量值!
            }
        }
    }
}

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3