从java中的二维数组流 [英] Stream from two dimensional array in java
问题描述
我试图从n维 int
数组中获取 IntStream
。有一个很好的API方法吗?
我知道两个流的连接方法。
I am trying to get an IntStream
out of an n dimensional int
arrays. Is there a nice API way to do it?
I know the concatenate method for two streams.
推荐答案
假设你想在行主要方法中按顺序处理数组数组,这应该可行:
Assuming you want to process array of array sequentially in row-major approach, this should work:
int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
IntStream stream = Arrays.stream(arr).flatMapToInt(x -> Arrays.stream(x));
首先它调用 Arrays.stream(T [])
方法,其中 T
被推断为 int []
,以获得 Stream< int []>
,然后 Stream#flatMapToInt()
方法映射每个 int []
元素到 IntStream
使用 Arrays.stream(int [])
方法。
First it invokes the Arrays.stream(T[])
method, where T
is inferred as int[]
, to get a Stream<int[]>
, and then Stream#flatMapToInt()
method maps each int[]
element to an IntStream
using Arrays.stream(int[])
method.
这篇关于从java中的二维数组流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!