Results 1 to 9 of 9
  1. #1
    JesusChrist is offline Member
    Join Date
    Jan 2014
    Location
    Heaven
    Posts
    1
    Rep Power
    0

    Smile 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. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default 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.
        }
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default 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. #5
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: 2D Boolean Array

    Quote Originally Posted by AlexGraal View Post
    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)) {
    }
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default Re: 2D Boolean Array

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

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default Re: 2D Boolean Array

    Quote Originally Posted by jim829 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,595
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: 2D Boolean Array

    Is a workaround really necessary in this situation?
    1000011 1100001 1101110 100000 1111001 1101111 1110101 100000 1110010 1100101 1100001 1100100 100000 1000010 1101001 1101110 1100001 1110010 1111001 111111

Similar Threads

  1. Replies: 6
    Last Post: 01-16-2014, 01:48 PM
  2. Thread not updating boolean array correctly
    By rmccleave559 in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-04-2013, 10:26 PM
  3. Star triangle with boolean array
    By helostcontrol in forum New To Java
    Replies: 1
    Last Post: 01-05-2012, 02:04 AM
  4. Star triangle with boolean array
    By helostcontrol in forum New To Java
    Replies: 1
    Last Post: 01-05-2012, 01:15 AM
  5. Boolean array Question
    By elm101 in forum New To Java
    Replies: 10
    Last Post: 06-01-2011, 07:02 AM

Posting Permissions

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