面向对象之集合ArrayList

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 面向对象集合
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个集合对象
            ArrayList list = new ArrayList();
            //集合:很多数据的一个集合
            //数组:长度不可变,类型单一
           //集合的优点:长度可以任意改变,类型也不固定。
            list.Add(1);
            list.Add(3.14);
            list.Add(true);
            list.Add("张三");
            list.Add('');
            list.Add(5000m);
            list.Add(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            person p = new person();
            list.Add(p);
            list.Add(list); //没有判断的情况下输出的是集合ArrayList的命名空间
            for (int i = 0; i < list.Count; i++)
            {
             //   Console.WriteLine(list[i]);
                if (list[i] is person)
                {
                    ((person)list[i]).Sayhello();
                }
                else if (list[i] is int[])
                {
                    for (int j = 0; j < ((int[])list[i]).Length; j++)
                    {
                        Console.WriteLine(((int[])list[i])[j]);
                    }
                }
                else
                {
                    Console.WriteLine(list[i]);//未判断
                }
                
                }
            Console.ReadLine();
        }
        public class person
        {
            public void Sayhello()
            {
                Console.WriteLine("我是人类");
            }
        }
    }
}
            list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }); //添加集合用AddRange
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine( list[i]);
            }
            Console.ReadLine();
原文地址:https://www.cnblogs.com/kangshuai/p/4695117.html