ArrayList1

来源:互联网 发布:学java web看什么书 编辑:程序博客网 时间:2024/06/07 22:38

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

namespace ArrayList1
{
    //创建一个学生类
    class Student
    {
        /// <summary>
        /// 无参构造函数
        /// </summary>
        public Student()
        { }
        /// <summary>
        /// 有参构造函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="age"></param>
        public Student(string name, int age)
        {
            this.name = name;
            this.age = age;
        }
        /// <summary>
        /// 姓名
        /// </summary>
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        /// <summary>
        /// 年龄
        /// </summary>
        private int age;
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //创建几个学生
            Student xyy = new Student("小月月", 18);
            Student fj = new Student("凤姐", 18);
            Student fr = new Student("芙蓉姐姐", 18);
            Student xl = new Student("犀利哥", 18);

            //创建一个集合
            ArrayList al = new ArrayList();
            //将创建的学生添加到集合中
            al.Add(xyy);
            al.Add(fj);
            al.Add(fr);
            al.Add(xl);
            //遍历集合中的学生
            //foreach (object item in al)
            //{
            //    Student student = item as Student;
            //    if (student != null)
            //    {
            //        Console.WriteLine(student.Name);
            //    }

            //}
            al.RemoveAt(0);
            for (int i = 0; i < al.Count; i++)
            {
                Student student = al[i] as Student;
                Console.WriteLine(student.Name);
                Console.WriteLine(student.Age);
            }
            Console.Read();
        }
    }
}

原创粉丝点击