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

更新时间:2020-11-23 15:15:27 点击次数:1104次
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、普通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());
}
}
}
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(对象类型 对象: 集合) ,其中对象类型建议使用泛型集合。
for(Object os : studentList){
    Student s = (Student) os;
System.out.println(s.getName());
System.out.println(s.getAge());
}
}
}
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);

//先创建一个List集合的迭代器,然后从中取集合对象。
Iterator it = studentList.iterator();
//it.hasNext()判断迭代器中有无下一个元素。
while(it.hasNext()){
Object os = it.next();
Student s = (Student) os;
System.out.println(s.getName());
System.out.println(s.getAge());
}
}
}
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);

//forEach( (参数(可多个))->{ 循环体 } )
studentList.forEach((os) -> {
Student s = (Student) os;
System.out.println(s.getName());
System.out.println(s.getAge());
});
}
}

本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

回到顶部
嘿,我来帮您!