我有Stream Stream<Integer> stream
// 1,2,3,4,5,6,7,8,9
我需要从此int[3][3]
stream
我该怎么做?
我试过
int[][] ints = stream
.map(i -> new int[]{i})
.toArray(int[][]::new);
但我明白了:[[1], [2], [3], [4], [5], [6], [7], [8], [9]]
但我需要:[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
编辑:我认为它不是dublicate this,因为我需要int [3] [3]数组而不是String [] []
我试过这个例子
int[][] array =
IntStream.range(0, 3)
.mapToObj(x -> IntStream.range(0, 3).boxed()
.toArray(Integer[]::new))
.toArray(int[][]::new);
我收到错误
Exception in thread "main" java.lang.ArrayStoreException: [Ljava.lang.Integer;
at java.util.stream.Nodes$FixedNodeBuilder.accept(Nodes.java:1222)
at java.util.stream.IntPipeline$4$1.accept(IntPipeline.java:250)
at java.util.stream.Streams$RangeIntSpliterator.forEachRemaining(Streams.java:110)
at java.util.Spliterator$OfInt.forEachRemaining(Spliterator.java:693)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:545)
at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:438)
at SparseMatrixSupportImpl.fromStream(SparseMatrixSupportImpl.java:25)
at SparseMatrixSupportImpl.fromStream(SparseMatrixSupportImpl.java:4)
at Main.main(Main.java:12)
编辑:
int[][] array = stream.collect(() -> new int[3][3],
(a, i) -> a[(i - 1) / 3][(i - 1) % 3] = i, (a, i) -> {
});
错误
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at SparseMatrixSupportImpl.lambda$fromStream$1(SparseMatrixSupportImpl.java:28)
at java.util.stream.ReduceOps$4ReducingSink.accept(ReduceOps.java:220)
at java.util.stream.IntPipeline$4$1.accept(IntPipeline.java:250)
at java.util.Spliterators$IntArraySpliterator.forEachRemaining(Spliterators.java:1032)
at java.util.Spliterator$OfInt.forEachRemaining(Spliterator.java:693)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:510)
at SparseMatrixSupportImpl.fromStream(SparseMatrixSupportImpl.java:27)
at SparseMatrixSupportImpl.fromStream(SparseMatrixSupportImpl.java:4)
at Main.main(Main.java:12)
int[] values = new int[]{1, 2, 3,5,6,7,8,9,11};
Stream<Integer> integerStream = Arrays.stream(values).boxed();
答案 0 :(得分:4)
您可以使用Java 9尝试此代码(这会在Java 8中引发编译错误,所以要小心):
int[][] array = Stream.iterate(1, i -> i < 10, i -> i + 1).collect(() -> new int[3][3],
(a, i) -> a[(i - 1) / 3][(i - 1) % 3] = i, (a, i) -> {});
Stream.of(array).forEach(a -> System.out.println(Arrays.toString(a)));
结果如下:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
使用Java 8:
int[][] array = IntStream.range(1, 10).collect(() -> new int[3][3],
(a, i) -> a[(i - 1) / 3][(i - 1) % 3] = i, (a, i) -> {});
Stream.of(array).forEach(a -> System.out.println(Arrays.toString(a)));
这是另一种支持除预定义序列(1,2,3,4,5,6,7,8,9)之外的数字的解决方案,但它不是一个干净的解决方案,因为它使用了一个计数器阵列:
int[] counter = {0};
int[][] array = Stream.of(4, 2, 3, 4, 5, 8, 9, 8, 11).collect(() -> new int[3][3],
(a, i) -> {
a[counter[0] / 3][counter[0] % 3] = i;
counter[0]++;
}, (a, i) -> {});
输出:
[4, 2, 3]
[4, 5, 8]
[9, 8, 11]
如果您想根据上述解决方案获得更多通用解决方案,可以尝试使用此代码段,该代码段允许设置可变数量的列:
List<Integer> list = Arrays.asList(4, 2, 3, 4, 5, 8, 9, 8, 11, 12, 13, 17, 32, 45, 89, 91, 91, 98, 87);
int[] counter = {0};
int cols = 5;
int rows = (int) Math.ceil(list.size() / cols) + 1;
int[][] array = list.stream().collect(() -> new int[rows][cols],
(a, i) -> a[counter[0] / cols][counter[0]++ % cols] = i, (a, i) -> {});
Stream.of(array).forEach(a -> System.out.println(Arrays.toString(a)));
上面的代码段打印:
[4, 2, 3, 4, 5]
[8, 9, 8, 11, 12]
[13, 17, 32, 45, 89]
[91, 91, 98, 87, 0]