s-day02-增强for循环遍历集合

This commit is contained in:
2026-07-30 16:57:35 +08:00
parent 149301c969
commit 36158e5e38
@@ -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<Integer> 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);
}
}
}