EnhancedFor

来源:互联网 发布:sip服务器 linux 编辑:程序博客网 时间:2024/06/07 11:27

EnhancedFor

增强for循环用于数组

//增强for循环用于数组int[] arr ={1,2,3,4,5};for(int i : arr){    System.out.println(i);}

缺点:不能方便的访问下标

增强for循环用于容器

Collection c = new ArrayList();c.add(new String("aaa"));c.add(new String("bbb"));c.add(new String("ccc"));```for(Object o : c){    System.out.println(o);```}

缺点:与Iterator相比,不能方便的删除集合中的内容

总结

除了简单遍历并读出其中的内容外,不建议使用增强For循环

原创粉丝点击