java中的EnhancedFor

import java.util.*;
public class EnhancedFor{
	public static void main(String[] args){
		int[] arr = {1, 2, 3, 4, 5};
		for(int i:arr){
			System.out.println(i);
		}
		
		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);
		}
	}
}

JDK1.5后才有的加强For。

缺点:

对于数组,不能方便的访问下值

对于集合,与用Iterator相比,不能方便的删除集合中的内容(其实在内部也是调用Iterator)

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

原文地址:https://www.cnblogs.com/mr-hu2009/p/java.html