import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /* * 增强for循环 * 可以遍历的内容有:数组,Collection,Map.但是Map不能直接遍历 * 结构: * for(元素:数组/Collection){ * 内容 * } * * 原理:每次遍历开始后,会自动从数组中依次取出一个元素放入前面的变量中,当次循环的操作使用的就是这个元素. * 遍历完成之后,会自动进行第二次遍历.一直到数组的末尾.所有元素遍历结束.循环停止. */ public class Demo1 { public static void main(String[] args) { //遍历数组 int[] arr = {4,5,7,9,9}; // for (int i : arr) { System.out.println(i); } //遍历Collection ArrayList<String> list = new ArrayList<>(); list.add("java"); list.add("java1"); list.add("java2"); list.add("java3"); list.add("java4"); System.out.println(list); for (String string : list) { System.out.println(string); } //遍历Map HashMap<String, String> map = new HashMap<>(); map.put("05", "iOS"); map.put("01", "java"); map.put("02", "html"); map.put("03", "BigData"); map.put("02", "iOS");//会将前面的值覆盖 for (String key : map.keySet()) { } for (Map.Entry<String, String> entry: map.entrySet()) { } System.out.println(map); } }