用数组存储三个学生对象,并遍历数组

package text;

import java.util.ArrayList;
import java.util.Collection;

public class StudentText {
public static void main(String[] args) {
//创建集合对象
    Collection c = new ArrayList();
    //创建学生对象
    Student s1 = new Student("林自如",15);
    Student s2 = new Student("王自健",36);
    Student s3 = new Student("林美嘉",18);
//并添加元素到集合
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());
}

}

}



猜你喜欢

转载自blog.csdn.net/xiaoxin1024/article/details/80325660