package cn.dali2.code02;
import java.util.ArrayList;
import java.util.Collection;
/*用for循环输出集合或者数组
* 在源码中我们可以看到Collection继承了一个Iterable的接口
* Collection<E> extends Iterable<E>
* Iterable接口就是允许对象成为foreach的目标
*
* 增强for循环的用法:
* for(集合或者数组中的数据类型 自定义变量名:集合或者数组的名称){
* sout
* }*/
public class Demo02 {
public static void main(String[] args) {
Collection<Integer> col = new ArrayList<>();
col.add(1);
col.add(5);
col.add(4);
col.add(3);
col.add(2);
for(int i : col){
System.out.println(i);
}
}
}
使用for循环遍历数组或者集合
最新推荐文章于 2023-11-03 15:58:47 发布