原帖地址:http://www.cnblogs.com/lihaozy/archive/2010/10/28/1863628.html
ArrayList是一种动态数组,其容量可随着我们的需要自动进行扩充.
ArrayList位于System.Collections命名空间中,所以我们在使用时,需要导入此命名空间.
//建立ArrayList对象
ArrayList students = new ArrayList();
//实例化几个Student类对象
Student rose = new Student("rose",25,"reading");
Student jack = new Student("jack",28,"singing");
Student mimi = new Student("mimi",26,"dancing");
//利用ArrayList类的add()方法添加元素
students.add(rose);
students.add(jack);
students.add(mimi);
//利用ArrayList的Count属性查看该集合中的元素数量
int number = students.Count;
Console.WriteLine("共有元素" + number + "个");
//读取单个元素,因为存入ArrayList中的元素会变为Object类型,
//利用foreach循环
foreach(Object o in students)
{
Student b = o as Student;
b.say();
}
600

被折叠的 条评论
为什么被折叠?



