List集合的四种遍历方式
为了便于理解,直接上代码,首先定义一个学生类,手动创建四个学生对象,并添加到list集合中:
public class TestList {
public static void main(String[] args) {
List studentList = new ArrayList();
Student s1 = new Student(“zhangsan”, 31);
Student s2 = new Student(“lisi”, 32);
Student s3 = new Student(“wangwu”, 33);
Student s4 = new Student(“zhaoliu”, 34);
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
}
}
class Student {
String name;
int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
1、普通for循环.
public class TestList {
public static void main(String[] args) {
List studentList = new ArrayList();
Student s1 = new Student(“zhangsan”, 31);
Student s2 = new Student(“lisi”, 32);
Student s3 = new Student(“wangwu”, 33);
Student s4 = new Student(“zhaoliu”, 34);
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
for (int i = 0; i < studentList.size(); i++) {
Student s = (Student) studentList.get(i);
System.out.println(s.getName());
System.out.println(s.getAge());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2、增强for循环:可以使用在遍历list集合、set集合和数组中.
public class TestList {
public static void main(String[] args) {
List studentList = new ArrayList();
Student s1 = new Student(“zhangsan”, 31);
Student s2 = new Student(“lisi”, 32);
Student s3 = new Student(“wangwu”, 33);
Student s4 = new Student(“zhaoliu”, 34);
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
for(Object os : studentList){
Student s = (Student) os;
System.out.println(s.getName());
System.out.println(s.getAge());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
3、迭代器循环, js中没有.
public class TestList {
public static void main(String[] args) {
List studentList = new ArrayList();
Student s1 = new Student(“zhangsan”, 31);
Student s2 = new Student(“lisi”, 32);
Student s3 = new Student(“wangwu”, 33);
Student s4 = new Student(“zhaoliu”, 34);
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
Iterator it = studentList.iterator();
while(it.hasNext()){
Object os = it.next();
Student s = (Student) os;
System.out.println(s.getName());
System.out.println(s.getAge());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
4、使用JDK1.8后的新技术,lambda表达式.
public class TestList {
public static void main(String[] args) {
List studentList = new ArrayList();
Student s1 = new Student(“zhangsan”, 31);
Student s2 = new Student(“lisi”, 32);
Student s3 = new Student(“wangwu”, 33);
Student s4 = new Student(“zhaoliu”, 34);
studentList.add(s1);
studentList.add(s2);
studentList.add(s3);
studentList.add(s4);
studentList.forEach((os) -> {
Student s = (Student) os;
System.out.println(s.getName());
System.out.println(s.getAge());
});
}
}