Java中List集合的四种常见遍历方法

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());
	});
}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值