gpt4 book ai didi

java - 如何检查java中列表中是否存在值

转载 作者:行者123 更新时间:2023-12-01 16:43:02 25 4
gpt4 key购买 nike

我想匹配 TaxList 中的值 199.04。下面是我的代码

List<BigDecimal> TaxList = new ArrayList<BigDecimal>();   

String NetP=driver.findElement(getNetPension).getAttribute("value");
float NetPension = new Float(NetP);
log.info("Value of Net Pension on screen : "+ NetPension);

if(TaxList.contains(NetPension))
{
log.info("Income tax value from the database is matching with the value on the screen");
}
else
{
log.warn("Both the values are different.Net pension is:" +NetPension );
}

我正在控制台中打印 TaxList

[199.04、610.72、122.12、866.52、94.56、143.48、78.28、132.6、12.9、32.03、1797.38、128.14、724.94]。即使值 199.04 存在。它会转到其他部分

两个值不同。养老金 Netty 为:199.04

最佳答案

在 ArrayList 上调用 contains 方法时,它会对所有包含的元素执行 equals() 调用。这里可能发生的情况是 BigDecimal 类的实例与 float 类型的值没有正确比较。

考虑进入 contains() 并检查与 BigDecimal 比较时对浮点值执行的操作。另一种可能的解决方案是自行将浮点值转换为 BigDecimal。

关于java - 如何检查java中列表中是否存在值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59214177/

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