需求:创建5个学生对象,将学生对象添加到集合中并遍历
1、创建学生类
2、创建学生对象集合
3、创建5个学生对象
4、将5个学生对象加入到集合中
5、获取迭代器对象
6、遍历迭代器
public class CollectionDemo5 {
public static void main(String[] args) {
//创建学生对象集合
Collection c1 = new ArrayList();
//创建学生对象
Student s1 = new Student("周杰伦", 20);
Student s2 = new Student("吴京", 21);
Student s3 = new Student("鹿晗", 22);
Student s4 = new Student("彭于晏", 23);
Student s5 = new Student("吴彦祖", 24);
//将5个学生对象加入到集合中
c1.add(s1);
c1.add(s2);
c1.add(s3);
c1.add(s4);
c1.add(s5);
//获取迭代器对象
Iterator iterator = c1.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
Student s = (Student) next;
System.out.println(s.getName() + "----" + s.getAge());
}
}
}
需求:利用数组存放3个学生的信息,并且遍历数组,获得每一位学生的信息
学生:Student
成员变量:name,age
构造方法:无参、有参
成员方法:setXxx()和getXxx()
toString()
1、创建学生类
2、创建学生对象数组,长度为3
3、创建3个学生对象,并赋值
4、把创建好的3个学生对象,放入到数组中
5、遍历数组
public class ObjectArrayDemo {
public static void main(String[] args) {
//创建学生对象数组,长度为3
Student[] arr = new Student[3];
for(int i=0;i


