简体   繁体   中英

Why don't Java logical operators && and || work with arrays?

I am effectively trying to write a multiple-bit if-statement in binary. If val is true, I would like to return the correct value of x. However, if val is false, I would like to return all values of the array to be false. This is effectively the same as checking all values within the array against the value of val. Why doesn't this work?

boolean[] ifStatement(bit0, bit1, bit2, bit3, val) {
    boolean[] x = {bit0, bit1, bit2, bit3};
    return (x && val);
}

In response to comments asking what I mean by the question, the following is a representation using an 8-bit AND gate in the program Logisim:

val关闭,返回false

val打开,返回原始输入值

I am also aware that it is very easy to do the same thing using a for statement. My question is why is the example code not possible?

Thanks in advance.

The short answer is no. You have check each item. It'd be kind of nice if it did work, but there you have it.

There are, however, special classes like BitSet that provide special functionality for big collections of booleans. You might find one of these useful. Typically not worth the effort for short numbers of booleans, though.

An array is a type all its own, not a boolean . So you can't use logical operators on an array type.

From your question description, it sounds like you just want to check every value in the array against some baseline boolean. You can do that with a loop over all elements in the array, and if any of don't match up to the baseline, return false.

public boolean ifStatement(boolean base, boolean... values) {
    for(boolean value : values) {
        if(!(value && base)) {
            return false;
        }
    }
    return true;
}

EDIT : After your diagram explained a few things, it seems that a boolean array is the wrong data structure. Why not work with a byte directly? (8 bits = 1 byte)

public byte ifStatement(boolean val, byte value) {
    return val ? value : (byte) 0x00;
}

You're either returning value or nothing in your example, so I believe that this would work better.

boolean[] x = {bit0, bit1, bit2, bit3};
return (x && val);

Why doesn't this work?

Your proposed meaning of that expression matches the semantics of the standard map higher-order function in the FP paradigm. Interestingly enough, I am not aware of any actual FP language which would have its AND operator overloaded to mean map((applyPartial(AND, val), coll) when one of its operands is a collection.

But, constraining the discussion to Java, such a feature would be deeply incongruent with everything else in it. It would make a logical operator suddenly behave like a batch collection operator; there is nothing even remotely close to that in Java, which is meant to be as unsurprising to beginners as possible, and especially so to people already familiar with C.

BTW you should have at least picked & because && has short-circuiting semantics, something which makes no sense in your proposal.

This doesn't work because the Java compiler doesn't interpret your wishes but adheres to its specification.

And the boolean operators only work with, well, booleans. Arrays of booleans aren't booleans.

Actually you don't need for loop / bitwise operators:

boolean []ifStatement(boolean b1, boolean b2, boolean b3, boolean b4, boolean val) {
    if (!val) {
        boolean []result = {false, false, false, false};
        return result;
    } else {
        boolean []result = {b1, b2, b3, b4};
        return result;
    }
}

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