1

我无法设法将其检索到数组或集合中,我该怎么做?

Stream<double[]> pyt = IntStream.rangeClosed(1, 100)
            .boxed().flatMap(a->IntStream.rangeClosed(a, 100)
                    .mapToObj(b->new double[]{a,b,Math.sqrt(a*a+b*b)})
                    .filter(t->t[2]%1==0));

这不起作用

double[] arr = (double[])pyt.toArray();
4

1 回答 1

0

目前尚不清楚您到底想要实现什么。目前你有一个double[]数组流。如果要将它们收集到double[][]数组中,则需要编写:

double[][] arr = IntStream.rangeClosed(1, 100)
                .boxed()
                .flatMap(a->IntStream.rangeClosed(a, 100)
                .mapToObj(b->new double[]{a,b,Math.sqrt(a*a+b*b)})
                .filter(t->t[2]%1==0))
                .toArray(double[][]::new);

如果要将其展平为double[]数组,则需要编写

double[] arr = IntStream.rangeClosed(1, 100)
                .boxed()
                .flatMap(a->IntStream.rangeClosed(a, 100)
                .mapToObj(b->new double[]{a,b,Math.sqrt(a*a+b*b)})
                .filter(t->t[2]%1==0))
                .flatMapToDouble(Arrays::stream)
                .toArray();
于 2015-12-08T07:47:22.893 回答