[英]printing 2D array using IntStream's flatMap method
I have a 2D array that I want to print using IntStream
. 我有一个我想用
IntStream
打印的2D数组。
this is the array, 这是阵列,
int[][] twoD = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
Now, using nested loop this can be done like, 现在,使用嵌套循环,这可以像,
for (int i = 0; i < twoD.length; i++) {
for (int j = 0; j < twoD[i].length; j++) {
System.out.println(twoD[i][j]);
}
}
but i want to use IntStream
. 但我想使用
IntStream
。 and I recently learned about its flatMap
method which I can use to achieve that, so I tried this, 我最近了解了它可以用来实现它的
flatMap
方法,所以我尝试了这个,
IntStream.range(0, twoD.length)
.flatMap(j -> IntStream.range(0, twoD[j].length))
.forEach(System.out::print);
and it outputs 010101
. 它输出
010101
。
One reason for the output being 010101
is that 010101
are index values not the values in the array and I have to map these values to the array values using something like, i -> twoD[i]
输出为
010101
一个原因是010101
是索引值而不是数组中的值,我必须使用诸如i -> twoD[i]
类的东西将这些值映射到数组值
so I tried this, 所以我试过这个,
IntStream.range(0, twoD.length)
.map(i -> twoD[i])
.flatMap(j -> IntStream.range(0, twoD[j].length))
.forEach(System.out::print);
but it gives error on map(i -> twoD[i])
, 但它在
map(i -> twoD[i])
上给出错误map(i -> twoD[i])
,
Type mismatch: cannot convert from int[] to int
but if it was 1D array then it would have worked, for example, 但如果它是1D阵列那么它会起作用,例如,
int[] oneD = { 1, 2, 3, 4, 5, 6 };
IntStream.range(0, oneD.length)
.map(i -> oneD[i])
.forEach(System.out::print);
How can I print the 2D array using the above approach ? 如何使用上述方法打印2D阵列?
I think you overcomplicate things. 我认为你过于复杂。 You could just do it like this:
你可以这样做:
Stream.of(twoD).flatMapToInt(IntStream::of).forEach(System.out::println);
What it does is: 它的作用是:
Stream<int[]>
from the int[][]
array int[][]
数组中获取Stream<int[]>
flatMap
each int[]
to an IntStream
so that you get back an IntStream
with all the elements of the 2D array flatMap
每个int[]
IntStream
到一个IntStream
以便返回一个IntStream
2D数组所有元素的IntStream
IntStream.range(0, twoD.length)
.mapToObj(i -> twoD[i])
.flatMapToInt(IntStream::of)
.forEach(System.out::print);
which produces the same output, but as you can see it's not very readable. 产生相同的输出,但你可以看到它不是很可读。 Here you don't need to stream the indices so the first approach with
flatMapToInt
is the best. 在这里,您不需要流式索引,因此使用
flatMapToInt
的第一种方法是最好的。
Now why your solution doesn't compile? 既然你的解决方案无法编译?
It's because map
on an IntStream
expect a mapping function that gives you back an int
but you give an int[]
. 这是因为
map
上的IntStream
预期的映射功能,让你回一个int
,但你给一个int[]
You need to use mapToObj
and then again flatMapToInt
to get an IntStream
and finally print the contents (this isn't the only solution though). 您需要使用
mapToObj
然后再使用flatMapToInt
来获取IntStream
并最终打印内容(尽管这不是唯一的解决方案)。
IntStream.range(0, twoD.length) .mapToObj(i -> twoD[i]) .flatMapToInt(IntStream::of) .forEach(System.out::print);
Do you gain in readability? 你有可读性吗? Not really, so I suggest to use the first approach which is clear and concise.
不是真的,所以我建议使用第一种清晰简洁的方法。
Note that the last solution could be also written as: 请注意,最后一个解决方案也可以写为:
IntStream.range(0, twoD.length) .flatMap(i -> IntStream.of(twoD[i])) .forEach(System.out::print);
... but I still prefer the first approach ! ......但我还是喜欢第一种方法! :)
:)
Why not stream the array itself: 为什么不流式传输数组:
Arrays.stream(twoD)
.flatMapToInt(Arrays::stream)
.forEach(System.out::println);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.