定义一个学生类,将学生导入集合,再将学生信息遍历出来
package practice;
import java.util.ArrayList;
import java.util.Collection;
public class P4 {
public static void main(String[] args){
Collection c=new ArrayList();
Student s1=new Student("林青霞",27);
Student s2=new Student("风清杨",30);
Student s3=new Student("令狐冲",33);
c.add(s1);
c.add(s2);
c.add(s3);
Object[] objs=c.toArray();
for(int x=0;x<objs.length;x++){
Student s=(Student) objs[x];
System.out.println(s.getname()+"------"+s.getage());
}
}
}
package practice;
public class Student {
private String name;
private int age;
Student(){}
Student(String n,int a){
//super();
this.name=n;
this.age=a;
}
public String getname(){
return name;
}
public int getage(){
return age;
}
}