1. Member
Join Date
Jan 2014
Location
Heaven
Posts
1
Rep Power
0

## 2D Boolean Array

Hello all,

I'm creating a 4x4 2D array. I need to be notified, or gain a value when all elements have been turned true. Is this as simple as "If the2DArray[] = true { blah blah }" ?

Thanks!

2. ## Re: 2D Boolean Array

No, Java doesn't work that way, you either test if all values in your matrix are 'true' or you count the number of elements that are set from 'false' to 'true' (or vice versa) each time you set one of the matrix elements.

kind regards,

Jos

3. ▼ dafuq did I do?
Join Date
Dec 2013
Posts
204
Rep Power
0

## Re: 2D Boolean Array

It is really simple actually.

Java Code:
```public static boolean allTrue(boolean[][] bools) {
for (int x = 0; x < bools.length; x++) { //Iterate through all x's
for (int y = 0; y < bools[0].length; y++) { //Iterate through all y's
if (!bools[x][y]) { //Check if the boolean at x,y is false
return false; //If it is, all of the boolean must not be true, return false;
}
}
}
return true; //If the boolean is still running, all booleans in bools must be true, return true.
}```

4. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Re: 2D Boolean Array

It's simple but not simple enough...

Would be great if you could do something like
Java Code:
```boolean[][] values = {{true,true,true},{true,true,true},{true,true,true}};
if(values[][] == true) System.out.println("All true!");```

5. ▼ dafuq did I do?
Join Date
Dec 2013
Posts
204
Rep Power
0

## Re: 2D Boolean Array

Originally Posted by AlexGraal
It's simple but not simple enough...

Would be great if you could do something like
Java Code:
```boolean[][] values = {{true,true,true},{true,true,true},{true,true,true}};
if(values[][] == true) System.out.println("All true!");```
Well, with that code I just provided, it is that easy, easier even:

Java Code:
```if (allTrue(values)) {
}```

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,738
Rep Power
10

## Re: 2D Boolean Array

As an alternative of arrays of booleans you may want to check out the BitSet class.

Regards,
Jim

7. ## Re: 2D Boolean Array

Originally Posted by jim829
As an alternative of arrays of booleans you may want to check out the BitSet class.
Hm, the shortest solution with a BitSet I can come up with is bitset.cardinality() == 16 (for a 4x4 boolean matrix), but under the hood the BitSet uses the Long.bitCount( ... ) method to count its number of set bits; quite hackish, compared to a simple checking loop ...

kind regards,

Jos

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,738
Rep Power
10

## Re: 2D Boolean Array

Yeah, I wasn't certain and I didn't look at the code. I just checked the API and it said something like "maintains a vector of boolean values."

Regards,
Jim

9. ▼ dafuq did I do?
Join Date
Dec 2013
Posts
204
Rep Power
0

## Re: 2D Boolean Array

Is a workaround really necessary in this situation?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•