简体   繁体   中英

If Statement in for loop only returns true once?

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;
    }

In this case

a = 17
maxCount = 2.0

and

tempPoint[2] in this case is 2.0 every time

but the debugger shows that

bestPoints.add(new Double[] {tempPoint[0], tempPoint[1]});

Only gets ran once as if the if statement was not true? Why?

You are very likely doing a reference equals rather than a comparison equals. When using the capital lettered versions of the numeric types, you should use one.equals(two) .

Ensure that the value of tempPoint[2] is exactly 2.0 . As a double , it's very possible that they are close, but not equal.

You also might find using a foreach loop easier to follow:

private List<Double[]> bestPoints(List<Double[]> includedPoints) {
    List<Double[]> bestPoints = new ArrayList<Double[]>();
    for (Double[] tempPoint : tempPoints) {
        if (tempPoint[2] == maxCount) {
             bestPoints.add(new Double[] { tempPoint[0], tempPoint[1] });
        }
    }

    return bestPoints;
}

If the values are always integers, then cast them before checking equality (eg, (long)tempPoint[2] == (long)maxCount ). If you want exact matches from double s, then continue what you are doing (after ensuring the proper type of equal check, dependent on maxCount), but if you want near matches (if decimals are a concern rather than predefined constants), then use an epsilon value:

public static boolean doubleEquals(double val1, double val2, double epsilon)
{
    return Math.abs(val1 - val2) < epsilon;
}

public static boolean doubleEquals(double val1, double val2)
{
    return doubleEquals(val1, val2, 1e-5);
}

Obviously specify an epsilon value that makes sense for you.

In the condition you are trying to match the references of the Double object instead you should use if(tempPoint[2].compareTo(maxCount)==0).

Since you are using "java.lang.Double". They are objects. Assuming maxCount is also Double

To compare two objects use

equals()

d1.equals(d2)

another option is to use "compareTo())

d1.compareTo(d2)

the value 0 if d1 is numerically equal to d2; a value less than 0 if d1 is numerically less than d2; and a value greater than 0 if d1 is numerically greater than d2.

Read more here http://download.oracle.com/javase/6/docs/api/java/lang/Double.html

To compare two double primitive values use compare(double d1, double d2) method of Double class. This is a static method. It returns 0 if both the values are equal, returns value less than 0 if d1 is less than d2, and returns value grater than 0 if d1 is grater than d2.

If you are quite sure that tempPoint[2] is always new Double(2.0) , and if maxCount is a primitive double, eg double maxCount = 2.0; rather than Double maxCount = new Double(2.0) , the if condition should succeed. Otherwise you may use if (tempPoint[2].equals(maxCount)) instead which does if (tempPoint[2].doubleValue() == maxCount.doubleValue())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM