Java中List的几种遍历方式(含Java8)
2019-01-13 本文已影响0人
YouMyGod
image
首先在Android Studio中建立一个Class,代码如下
class Test {
public static ListmemberList =new ArrayList<>();
public Test() {
}
public void addMember(int a){
for (int i =0 ; i <15; i ++){
memberList.add(a+i);
}
}
public static void main(String[] args){
Test test =new Test();
test.addMember(5);
// 遍历第一种方法
for (int x :memberList){
![](https://pocket-image-cache.com/direct?url=https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1*Ipne1YyFQI09V_Y4UiKWgg.jpeg&resize=w1206)
System.out.println(x);
}
System.out.println("==============================");
//遍历第二种方法
for( Iterator itr =memberList.iterator(); itr.hasNext();){
int temp = itr.next();
System.out.println(temp);
}
**System.out.println("==============================");
//遍历第三种
memberList.forEach(x->{System.out.println(x);});
}
}
运行结果: