简体   繁体   中英

Insert parameter depending on boolean value

Is it possible to insert a parameter in a function depending on a boolean value?

For example, I have this piece of code:

Math.min(boolA ? doubleValueA, boolB ? doubleValueB);

Thanks in advance!

Use a default value (such as Double.MAX_VALUE) if boolA or boolB is false:

Math.min(
    (boolA) ? doubleValueA : Double.MAX_VALUE,
    (boolB) ? doubleValueB : Double.MAX_VALUE
);

Edit

If you have a list of variables that you want to find the minimum, but only if the corresponding boolean variable is set, load the list into an array and find the minimum:

ArrayList<Double> myArray = new ArrayList<Double>();
if (boolA) myArray.add(doubleValueA);
if (boolB) myArray.add(doubleValueB);
// etc

double minValue = Double.MAX_VALUE; // start with largest possible value
// loop through and replace with any smaller values
for (double val : myArray)
    if (val < minValue) minValue = val;
// val now contains the smallest value

Edit 2

This can also be applied in general to other functions. Selectively add your parameters to an array, and pass the array to the function. If you have control over the function definition, you can use variable arguments to make it simpler.

public double MyMin(double... myArray) {
    double minValue = Double.MAX_VALUE; // start with largest possible value
    // loop through and replace with any smaller values
    for (double val : myArray)
        if (val < minValue) minValue = val;
    // val now contains the smallest value
    return val;
}

java does not have operator like

boolA ? doubleValueA

but supports ternary operator

boolA ? doubleValueA : defaultValue

it means that if boolA is true, then use doubleValueA otherwise use defaultValue . Otherwise

Math.min(boolA ? doubleValueA, boolB ? doubleValueB);

would be pointless if boolA is false (except for special processing and going with null in this case, but Java does not)

altogether, you should change to

Math.min(boolA ? doubleValueA : defaultA, boolB ? doubleValueB : defaultB);

If you are using the ternary operator, you need a false condition. Say you want to make sure that if boolA is false then doubleValueB will be min while if boolB is false doubleValueA will be min. You could make the other condition be the max integer and then this will be the case.

Of course if both boolA and boolB are false then your value will be the max integer value. If that can ever happen, then you'll need additional code to handle it.

Math.min(boolA ? doubleValueA : Integer.MAX_VALUE, boolB ? doubleValueB : Integer.MAX_VALUE);

Note that Math.min(int, int) does not take varargs. If you want to use a variable-length argument list as described here you'll need your own min function with varargs.

if(boolA && boolB) {
    Math.min(doubleA, doubleB);
} else if(boolA) {
    doubleA;
} else if(boolB) {
    doubleB;
} else {
    ????
}

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