【发布时间】:2011-04-28 05:02:46
【问题描述】:
private List<Double[]> bestPoints(List<Double[]> includedPoints) {
List<Double[]> bestPoints = new ArrayList<Double[]>();
int a = includedPoints.size();
for (int i = 0; i < a; i++) {
Double[] tempPoint = includedPoints.get(i);
if (tempPoint[2] == maxCount) {
bestPoints.add(new Double[] {tempPoint[0], tempPoint[1]});
}
}
return bestPoints;
}
在这种情况下
a = 17
maxCount = 2.0
和
tempPoint[2] 在这种情况下每次都是2.0
但调试器显示
bestPoints.add(new Double[] {tempPoint[0], tempPoint[1]});
如果 if 语句不正确,只运行一次?为什么?
【问题讨论】:
-
你确定它是 2.0,而不是 1.999999999...?在
double上使用==是非常危险的操作。 -
他没有在
double上使用 == ...他在Double上使用它,这就是问题所在。它是在比较参考值。
标签: java for-loop if-statement