java8 遍历数组的几种方式

有如下一个String数组
String[] array = {"a", "b", "c", "d", "e"};

1.根据下标遍历

for(int i = 0; i < 5; i++){
<span style="white-space:pre">	</span>    System.out.println(array[i]);
}

2.foreach遍历

for(String x : array){
            System.out.println(x);
}

3.迭代器遍历

List list = Arrays.asList(array);
        Iterator iterator = list.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

4.stream遍历

Arrays.asList(array).stream().forEach(x -> System.out.println(x));
也可以这样写:
Arrays.asList(array).stream().forEach(System.out::println);


版权声明:本文为qq_27068845原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。