import java.util.ArrayList;
import java.util.List;
public class MutilIterationDemo {
public static List<String> list=null;
public static void main(String[] args) {
//添加數據
list = addData();
//多種形式遍歷數據
forEach(list);
}
private static void forEach(List<String> list) {
// TODO Auto-generated method stub
System.out.println("====for數組遍歷====");
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i)+" ");
}
System.out.println("\n====for臨時遍歷====");
for (String str:list) {
System.out.print(str+" ");
}
System.out.println("\n====forEach遍歷====");
list.forEach(c->{
System.out.print(c+" ");
});
System.out.println("\n====while遍歷====");
int len = list.size();
while(len>0){//從後往前遍歷
System.out.print(list.get(--len)+" ");
}
System.out.println("\n====and so on====");
}
private static List<String> addData() {
// TODO Auto-generated method stub
list = new ArrayList<String>();
list.add("張三");
list.add("李四");
list.add("王五");
list.add("老六");
return list;
}
}java