用于遍历数组和集合,是for循环的增强版本
注意:在用foreach循环遍历一个集合时,不能改变集合中的元素,如增加元素、修改元素。否则会抛出ConcurrentModificationException异常
格式如下:
for(元素类型 元素变量:遍历对象(数组或集合)){
引用元素变量x的语句;
}
遍历数组
public static void main(String[] args) {
int[] a={0,1,2,3,4,5};
for(int x:a){
System.out.println(x);
}
}
遍历List
List<Map<String, Object>> parameterList;
for(Map<String, Object> stu:parameterList) {
System.out.println("=====>" + stu.get("SJ"));
}