在JDK8中,由于引入了Stream,所以可以很方便的遍历数组
1.采用lambda表达式
int[] add2 = new int[] {1,2,3,4};
Arrays.stream(add2).forEach(x -> System.out.println(x));
2.采用双冒号写法
int[] add2 = new int[] {1,2,3,4};
Arrays.stream(add2).forEach(System.out::println);
此时,将方法作为参数,传递到stream中,将stream中的每个元素都传入到该方法里面执行一下。
类名::方法名