Akış
stream
den int[3][3]
oluşturmak gerek Stream<Integer> stream
// 1,2,3,4,5,6,7,8,9
Nasıl Yaptım?
Ben
int[][] ints = stream
.map(i -> new int[]{i})
.toArray(int[][]::new);
çalıştı Ama olsun:
[[1], [2], [3], [4], [5], [6], [7], [8], [9]]
Ama gerekir: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
EDIT: Ben int gerek çünkü this Dublicate olmadığını düşünüyorum [3] [3] dizi ve değil [] []
Bu örneği denedim
int[][] array =
IntStream.range(0, 3)
.mapToObj(x -> IntStream.range(0, 3).boxed()
.toArray(Integer[]::new))
.toArray(int[][]::new);
Ve olsun hata
DÜZENLEMEException 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) -> {
});
hata Sen Java 9 ile bu kodu deneyebilirsiniz
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();
Olası yinelenen (https://stackoverflow.com/questions/26050530/filling-a-multidimensional-array-using-a-strea m) –
@AntonBalaniuc Akış zaten mevcutsa çalışmayabilir, ör. bazı kaynaklardan geliyor OP değiştiremiyor. –