diff --git a/s-day02/src/com/inmind/foreach03/Demo05.java b/s-day02/src/com/inmind/foreach03/Demo05.java new file mode 100644 index 0000000..aae50ff --- /dev/null +++ b/s-day02/src/com/inmind/foreach03/Demo05.java @@ -0,0 +1,27 @@ +package com.inmind.foreach03; + +import java.util.ArrayList; +import java.util.Collection; + +/* +增强for循环遍历集合 +注意:集合的foreach循环本质是迭代器,那么所有的单列集合都可以用foreach遍历 + */ +public class Demo05 { + public static void main(String[] args) { + //单列集合的多态写法 + Collection cols = new ArrayList<>(); + cols.add(1); + cols.add(2); + cols.add(3); + cols.add(4); + //遍历集合,无法使用普通for循环遍历 + for (Integer i : cols) { + System.out.println(i); + } + + for (Integer element : cols) { + System.out.println( element); + } + } +}