JAVA单排日记-2019/12/23-1-增强for循环

增强for循环:for循环的格式,简化了迭代器的书写,对数组或者Collection集合的遍历

格式

for(数组/集合的数据类型 变量名:数组/集合名){
	sout(变量名);
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

public class Demofor {
    public static void main(String[] args) {
        Collection<String> coll = new ArrayList<>();
        coll.add("1");
        coll.add("2");
        coll.add("3");
        coll.add("4");

        int[] a = {5, 6, 7, 8};

        for (String i : coll) {
            System.out.println(i);
        }

        for (int j : a) {
            System.out.println(j);
        }
    }
}

在这里插入图片描述

发布了90 篇原创文章 · 获赞 1 · 访问量 2064

猜你喜欢

转载自blog.csdn.net/wangzilong1995/article/details/103671771