您的位置:首页 > 编程语言 > C#

c#ArrayList的用法

2015-08-07 19:31 274 查看
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);//遍历方法二




            }


        }


    }


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: