简体   繁体   中英

Operation to “Get and AND” boolean variable in Java

Is it possible in Java to use syntax like ( i++ , ++i ) for boolean logic operators?

I have a boolean variable that is true only for the first iteration of a foreach loop. That iteration has to be skipeed.

Full syntax is

for (...)
{
    if (bool)
    {
        bool &= false;
        continue;
    }
}

I wonder if there is any way to shorten the syntax without using AtomicBoolean . For example construct if (bool &= false) is syntactically correct but I think it will compare the final result and not the original value.

Google is not my friend because the search query is misleading

Personally I would simplify your current code to:

for (...)
{
    if (bool)
    {
        bool = false;
        continue;
    }
    // Rest of code
}

... but if you really want to do it in the if condition as a side-effect, you could use:

for (...)
{
    if (bool && !(bool = false))
    {
        continue;
    }
    // Rest of code
}

Here the first operand of the && operator covers subsequent operations, and !(bool = false) will always evaluate to true and set bool to false.

Another option, from comments:

for (...)
{
    if (bool | (bool = false))
    {
        continue;
    }
    // Rest of code
}

This performs the assignment on each iteration, but it still gives the right result each time.

I really, really wouldn't use either of these last two options though.

Your code is the usual thing to do. However, there's an alternative:

for (SomeType thing : Iterables.skip(things, 1)) {
    // process thing
}

This uses Google Guava's Iterables.skip() method and produces your expected output - a for-each loop iterating over the collection and skipping the first element.

Alternatively, just use an integer variable and use ++ to post-increment it.

int iter = 0;
for (...) {
    if (iter++ == 0) {
        continue;
    }
    ...
}

If you want to skip the first iteration, this might even be easier to understand.

Don't use increments for boolean types If you must use a boolean, either toggle it, such as !bool , or just set it to false:

for (...){
    if (bool) {
        bool = false;
        continue;
    }
}

Ideally, if all you want is to skip the first, last or nth iteration, do not use a boolean at all but an int instead ...

    int skipIndex = 0;
    for(int index=0; index < 5; index++){
        if(index != skipIndex) {
            System.out.println(index);
        }
    }

... or the following to only skip the first iteration:

    int[] values = new int[]{0, 1, 2, 3, 4};
    for (int index = 1; index < values.length; index++) {
        System.out.println(values[index]);
    }       

If (and only if) you are really so certain that you will always need to continue on the first iteration, why not just skip that iteration? Instead of starting with i=0 , start with

for(i=1....

I've been struggling to see why the OP is trying to use bool &= false; when bool = false will obviously do. In that sense, Jon Skeet 's answer is (unsurprisingly) correct.

What I think the OP actually wants to do is set the variable to false and test it in one step. That's the reason for the reference to AtomicBoolean . It's nothing per-se to do with loops. IE he wants to do the same as:

int a=0;
for ( ... ) {
    if (a++ == 0 ) { // works if we aren't doing too many iterations
        continue;
    }
    ...
}

ie he wants the equivalent of a post-increment operator.

If I'm right, it's not the loop he's worried about, it's the fact that a here is being read once, then separately read again.

This is a case of premature optimisation. The Java compiler is very likely (no I haven't tested it) to produce a single read and test and of the code with

boolean a=false;
for ( ... ) {
    if (!a) {
        a = true;
        continue;
    }
}

as Jon Skeet suggested.

The answer (for completeness) is that there is no post-increment operator that works on boolean and I couldn't work out how to define a function that does that without at least mentioning the variable twice. However, that should not be a design consideration.

Note in a real for loop you can just do:

int i;
boolean skip;
for (i=0, skip=true; i<10; i++, skip=false) {
    if (skip)
        continue;
}
for (int i = 0, boolean doIt = false; i < 10; i++, doit = true) {
    if (doIt) {
        doStuff();
    }
}

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