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