import java.util.ArrayList;
import java.util.Iterator;
public class Test1 {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("任",23));
students.add(new Student("我",24));
students.add(new Student("行",25));
System.out.println("简单for循环");
for(int i=0;i<students.size();i++){
Student student = students.get(i);
System.out.println("学生姓名:"+student.getName()+",学生年龄"+student.getAge());
}
System.out.println("迭代器");
Iterator<Student> iterator = students.iterator();
while(iterator.hasNext()){
Student student=iterator.next();
System.out.println("学生姓名:"+student.getName()+",学生年龄"+student.getAge());
}
System.out.println("foreach方法");
for(Student student:students){
System.out.println("学生姓名:"+student.getName()+",学生年龄"+student.getAge());
}
}
}
三种循环遍历输出学生类
最新推荐文章于 2023-11-01 10:01:09 发布