import java.util.ArrayList;
import java.util.Collection;
public class T {
public static void main(String[] args) {
Collection stu=new ArrayList();
stu.add(new Student("jack",20));
stu.add(new Student("rose",22));
stu.add(new Student("joker",21));
stu.add(new Student("mike",24));
stu.add(new Student("vey",19));
//for遍历
Object [] objects=stu.toArray();
for (int i = 0; i <objects.length ; i++) {
Student student=(Student)objects[i];//向下转型
System.out.println(objects[i]);
}
//迭代遍历
Iterator iterator=stu.iterator();
while(iterator.hasNext()){
//方法一,利用学生类使用方法,可以不重写toString
Student student=(Student) iterator.next();
System.out.println(student.getName()+","+student.getAge());
//方法二,直接遍历,已经重写过toString
//System.out.println(iterator.next());
}
}
}
class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/*@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}*/
}