如果只是遍历使用superfor比较方便,如果是需要到索引就用普通for;
package com.cn;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class SuperforDemo {
public static void main(String[] args) {
//creat the object about list
List<Student> list = new ArrayList<>();
//creat the Student about list
Student s1 = new Student("ytt",12);
Student s2 = new Student("ytt",12);
Student s3 = new Student("ytt",12);
Student s4 = new Student("ytt",12);
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
//1\迭代器
Iterator<Student> it = list.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName()+"--"+s.getAge());
}
//2\
for(Student s :list){
System.out.println(s.getName()+"--"+s.getAge());
}
}
}