使用IntStream的flatMap方法打印2D数组 [英] printing 2D array using IntStream's flatMap method
问题描述
我有一个2D数组,我想用 IntStream
打印。
这是数组,
int [] [] twoD = {{1,2},{3,4},{5,6}};
现在,使用嵌套循环可以这样做,
for(int i = 0; i< twoD.length; i ++){
for(int j = 0; j< twoD [i]。长度; j ++){
System.out.println(twoD [i] [j]);
}
}
但我想使用 IntStream
。我最近了解了它可以用来实现它的 flatMap
方法,所以我尝试了这个,
IntStream.range(0,twoD.length)
.flatMap(j - > IntStream.range(0,twoD [j] .length))
.forEach(的System.out ::打印);
并输出 010101
。
输出 010101
的一个原因是 010101
索引值不是数组中的值,我必须使用诸如 i - >之类的内容将这些值映射到数组值。这是我试过的,
IntStream.range(0,twoD.length)
.map(i - > twoD [i])
.flatMap(j - > IntStream.range(0,twoD [ j] .length))
.forEach(System.out :: print);
但它在地图上出错(i - > twoD [i] )
,
类型不匹配:无法从int []转换为int
但是如果它是一维数组那么它就可以工作,例如,
int [] oneD = {1,2,3,4,5,6};
IntStream.range(0,oneD.length)
.map(i - > oneD [i])
.forEach(System.out :: print);
如何使用上述方法打印2D数组?
我认为你过于复杂。你可以这样做:
Stream.of(twoD).flatMapToInt(IntStream :: of)。forEach(System .OUT ::的println);
它的作用是:
- 从
int [] []
数组<获取Stream< int []>
/ li>
-
flatMap
每个int []
到IntStream
这样你就可以获得每个元素的IntStream
以及2D数组的所有元素 - ,打印它
您想要做的是可实现但不易阅读。嵌套循环的正式翻译将是:
IntStream.range(0,twoD.length)
.forEach (i - > IntStream.range(0,twoD [i] .length)
.forEach(j - > System.out.println(twoD [i] [j])));
产生相同的输出,但正如你所看到的那样,它的可读性不高。在这里你不需要流式索引,所以第一种方法 flatMapToInt
是最好的。
现在为什么你的解决方案无法编译?
这是因为 map
在 IntStream
上需要映射函数,它返回 int
,但你给 int []
。您需要使用 mapToObj
,然后再使用 flatMapToInt
来获取 IntStream
最后打印内容(这不是唯一的解决方案)。
IntStream.range(0,twoD。长度)
.mapToObj(i - > twoD [i])
.flatMapToInt(IntStream :: of)
.forEach(System.out :: print);
您是否获得了可读性?不是真的,所以我建议使用第一种简洁明了的方法。
注意最后一个解决方案也可以写成:
IntStream.range(0,twoD.length)
.flatMap(i - > IntStream.of(twoD [i]))
.forEach(System.out :: print);
...但我还是更喜欢第一种方法! :)
I have a 2D array that I want to print using IntStream
.
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
. and I recently learned about its flatMap
method which I can use to achieve that, so I tried this,
IntStream.range(0, twoD.length)
.flatMap(j -> IntStream.range(0, twoD[j].length))
.forEach(System.out::print);
and it outputs 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]
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])
,
Type mismatch: cannot convert from int[] to int
but if it was 1D array then it would have worked, for example,
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 ?
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:
- get a
Stream<int[]>
from theint[][]
array flatMap
eachint[]
to anIntStream
so that you get back anIntStream
with all the elements of the 2D array- for each element, print it
What you want to do is achievable but not very readable. A formal translation of your nested loop would be:
IntStream.range(0, twoD.length)
.forEach(i -> IntStream.range(0, twoD[i].length)
.forEach(j -> System.out.println(twoD[i][j])));
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.
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[]
. 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).
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 ! :)
这篇关于使用IntStream的flatMap方法打印2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!