我正在学习 Stream,我尝试使用 Arrays 类中找到的 asList 方法打印 int Array,不幸的是我得到了错误的结果。
有人可以向我解释为什么我会得到这个错误的结果吗?
public class array {
public static void main(String[] args) {
/*my way*/
int [] array = new int[]{1,2,3,7,1};
Arrays.asList(array).stream().forEach(System.out::println);
System.out.println();
/*the good way*/
Arrays.stream(array).forEach(System.out::print);
}
}
结果:
[I@3e3abc88
12371
最佳答案
Arrays.asList(array) -> a List<int[]>
Arrays.asList(array).stream() -> Stream<int[]>
因此 Stream 中的每个元素都是 int[]
,而不是 int
;所以你正在尝试打印一个数组(Object
);这不会像你想象的那样工作。
在第二个示例中:
Arrays.stream(array -> IntStream
因此这是有效的
关于java - 使用 Arrays.asList 方法通过 Stream 打印 int Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52278824/