package java_test; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class ForeachDemo { public static void main(String[] args) { /* * 增强for循环。作用:用于遍历Collction集合or数组. * 格式: * for(元素类型变量:Collection容器or数组) * { * } */ Collection coll=new ArrayList(); coll.add("abc1"); coll.add("abc2"); coll.add("abc3"); for(Object obj : coll){ System.out.println(obj); } /*迭代器去除方法 for (Iterator it = coll.iterator(); it.hasNext();) { Object obj = (Object) it.next(); System.out.println(obj); } */ //对于数组的遍历,如果不操作角标,可以使用增强for循环;如果操作角标,使用普通for循环 int [] arr= {23,43,123,123}; for(int a : arr){ System.out.println(a); } } }