码迷,mamicode.com
首页 > 其他好文 > 详细

面向对象之集合ArrayList

时间:2015-08-02 10:09:30      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:

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();

 

面向对象之集合ArrayList

标签:

原文地址:http://www.cnblogs.com/kangshuai/p/4695117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!