gpt4 book ai didi

java - 如何使用流从 double 组中过滤掉非整数值?

转载 作者:行者123 更新时间:2023-12-01 12:55:41 24 4
gpt4 key购买 nike

IntelliJ 一直告诉我 Double[] 是一个错误:方法引用中的返回类型错误:无法将 Double[] 转换为 A[]。我使用了 double[],删除了::new 静态引用,但仍然出现相同的错误。如何解决此错误?我试图在过滤结果中取回 1.000。

double[] pricesDoubleArr = {1.000,2.800,4.900};
double[] pricesDoubleFiltered = Arrays.stream(pricesDoubleArr)
.filter(x -> x != Math.round(x))
.toArray(Double[]::new);

最佳答案

只需使用 .toArray();没有 Double[]::new ,因为您的流从一开始就包含一个 double 数组:

double[] doubles = Arrays.stream(pricesDoubleArr)
.filter(x -> x != Math.round(x))
.toArray();

我不确定你从代码中看什么,但从你的问题来看:

I am attempting to get back 1.000 in the filtered results.



我认为您正在寻找:
.filter(x -> x == (double) (int) x)

关于java - 如何使用流从 double 组中过滤掉非整数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60559549/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com