Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: comparing values in a boolean array

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default comparing values in a boolean array

    Hi,

    I am trying to figure out how compare two values in a boolean array. For example:

    if [i] = true, compare [i+1] and [i-1]... then, if one is true, and the other is false, [i] = true.

    Here's a snippet of what I am working on:

    public static void theLivingDead(boolean[] villagers) {

    for (int i=0; i<villagers.length; i++) {
    if (villagers[i] == true) {
    if (villagers[i++] == true && villagers[i--] == false) {
    villagers[i] = false;
    }
    else {
    villagers[i] = true;
    }
    }
    else {
    if (villagers[i++] == true && villagers[i--] == false) {
    villagers[i] = true;
    }
    else {
    villagers[i] = false;
    }
    }

    }
    }

    You can probably see what I am trying to do by reading the code, however, I am clearly missing something.

  2. #2
    Junior Member
    Join Date
    Apr 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Boolean array comparison/arguments

    Hi,

    I am trying to figure out how compare two values in a boolean array. For example:

    if [i] = true, compare [i+1] and [i-1]... then, if one is true, and the other is false, [i] = true.

    Here's a snippet of what I am working on:

    public static void theLivingDead(boolean[] villagers) {
     
          for (int i=0; i<villagers.length; i++) {
                if (villagers[i] == true) {
                    if (villagers[i++] == true && villagers[i--] == false) {
                        villagers[i] = false;
                    }
                   else {
                        villagers[i] = true;
                   }
         }
         else {
              if (villagers[i++] == true && villagers[i--] == false) {
                  villagers[i] = true;
              }
              else {
                  villagers[i] = false;
              }
         }
     
    }
    }

    You can probably see what I am trying to do by reading the code, however, I am clearly missing something.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Boolean array comparison/arguments

    how compare two values in a boolean array
    You can use the == operator to compare two boolean values:
       if(theArray[i] == theArray[idx2])  { // compare two values in an array

    Note: you do not need to use == true to test a boolean variable. The variable's contents can be used directly:
       if(boolVal == true) // poor technique
     
       if( boolVal)   // better to use directly
    An if statement takes a boolean expression and a boolean variable works there.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Apr 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean array comparison/arguments

    in
    if(theArray[i] == theArray[idx2])  { // compare two values in an array

    'idx2' would be a seperate index, correct?

    I am trying to compare three 'side by side' values..

    say for instance, [3] = true, I want to see whether or not [2] is true/false, and also whether or not [4] is true/false ((((within the same array)))).... and be able to keep, or alter the value of [3]....

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: comparing values in a boolean array

    compare three 'side by side' values.
    There are only two operands in any comparison.
    With three different binary values there can be 8 different combinations: T,T,T to F,F,F
    Which of those 8 combinations do you want to test for?


    'idx2' would be a seperate index, correct?

    I am trying to compare three 'side by side' values..
    idx could be i+1
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: comparing values in a boolean array

    I'm not sure now, honestly, though I believe I would want to test all of them. This is how the algorithm reads:

    if the current villager is alive:
    * if only one neighbor is alive, the current villager is dead in the next generation.
    * otherwise,the current villager is alive in the next generation.
    *
    otherwise (the current villager is dead):
    * if only one neighbor is alive, the current villager is alive in the next generation.
    * otherwise, the current villager is dead in the next generation.
    *

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: comparing values in a boolean array

    I would want to test all of them
    Ok, that could be 8 if statements, one for each case.
    For example to test boolArray's contents for
    case 1: all true:
    if(boolArray[i] && boolArray[i+1] && boolArray[i+2])
    and case 8: all false:
    if(!boolArray[i] && !boolArray[i+1] && !boolArray[i+2])
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: comparing values in a boolean array

    I have managed to see a positive change within the last few moments of your time, Norm, Thank you! Although I have managed to breach a wall, thanks to you, I believe now I have quite a-ways to get yet. Here's what my code looks like now:

     for (int i=0; i<villagers.length; i++) {
    		 	 	 if (villagers[i]) {
    		 	 	 	 if (villagers[i+1] && !villagers[i-1]) {
    		 	 	 	 	 villagers[i] = false;
    		 	 	 	 }
    		 	 	 	 else {
    		 	 	 	 	 villagers[i] = true;
    		 	 	 	 }
    		 	 	 }
    		 	 	 else {
    		 	 	 	 if (villagers[i+1] && !villagers[i-1]) {
    		 	 	 	 	 villagers[i] = true;
    		 	 	 	 }
    		 	 	 	 else {
    		 	 	 	 	 villagers[i] = false;
    		 	 	 	 }
    		 	 	 }

    I will provide tests for the different combinations, and report back. Thanks again for the help!

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: comparing values in a boolean array

    Ok, I'm glad you are making progress.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Returning Boolean Values
    By triumvirate1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 7th, 2013, 02:40 AM
  2. Replies: 4
    Last Post: February 17th, 2012, 07:05 PM
  3. Comparing Input to array
    By hbonh in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2011, 10:43 AM
  4. [SOLVED] boolean values are going against my assumption on execution of events
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 28th, 2011, 12:53 PM
  5. Printing boolean values from an array
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 12:11 AM