使用for循环遍历数组或者集合

package cn.dali2.code02;

import java.util.ArrayList;
import java.util.Collection;

/*用for循环输出集合或者数组
* 在源码中我们可以看到Collection继承了一个Iterable的接口
* Collection extends Iterable
*     Iterable接口就是允许对象成为foreach的目标
*
* 增强for循环的用法:
*   for(集合或者数组中的数据类型 自定义变量名:集合或者数组的名称){
*   sout
*   }*/
public class Demo02 {
    public static void main(String[] args) {
        Collection 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);
        }
    }
}

你可能感兴趣的:(JAVA)