List的几种遍历方式

public static void main(String[] args) {

        List list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        //普通for循环
        for (int i = 0; i < list.size(); i++) {
            System.out.println("item = " + list.get(i));
        }
        //增强for循环
        for (String s : list) {
            System.out.println("item = " + s);
        }
        //迭代器遍历
        Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            String item = iterator.next();
            System.out.println("item = " + item);
        }

        //forEach遍历
        list.forEach(item->{
            System.out.println("item = " + item);
        });

    }

 

你可能感兴趣的:(java)