Question:
Java boolean problem?
2010-01-12 10:45:06 UTC
I need to set the boolean I have to = false when it starts up and then when I click a button. What is the coding to do this? I have an array of booleans.
boolean[] Holder = new boolean[6];
now how to i set to false?
Five answers:
?
2010-01-15 09:23:48 UTC
FYI, the very first time you create a boolean array in Java, all its elements are set to 'false' by default.



if you want to reset the array every now and then, you should write a method.



private static final void reset(boolean[] arr) {



for (int i = 0; i < arr.length; ++i) {



arr[i] = false;



}



}



every time you want to reset the array, call the method



reset(Holder);
deonejuan
2010-01-12 12:07:06 UTC
boolean[] holder = new boolean[ 6 ]; // equals f,f,f,f,f,f when you create it

I can think of 8 or 10 ways to flip booleans, here is 3



import java.util.Arrays;



public class BooleanTricks {



public static void main(String[] args) {

System.out.println("init a boolean array, default vals are false");

boolean [] holder = new boolean[ 6 ];

int count = -1;

for (boolean b : holder) {

System.out.println("holder[ " + ++count + "] = " + b);

}

System.out.println("\nwith the java API Arrays.method()\n");

Arrays.fill( holder, true );

count = -1;

for (boolean b : holder) {

System.out.println("holder[ " + ++count + "] = " + b);

}

System.out.println("\nwith the uninary '!', flips like a light switch");

count = -1;

holder[ 4 ] = !holder[ 4 ];

for (boolean b : holder) {

System.out.println("holder[ " + ++count + "] = " + !b);

}



}

}
Leo D
2010-01-12 10:56:38 UTC
Set all of them to false? Do you mean?



for (int i = 0; i < Holder.length, i++) {

Holder[i] = false;

}



Now, it's probably a bad idea to start a name with a capital letter and then lower-cases unless it's a class name. (Holder looks like a class name, but holder looks like a variable name. It's not technically required in Java, but it's a good idea.)



---



Also, depending on what you're trying to do, you might actually find it easier to work with an Integer and use bit-wise operators instead of an Array of Booleans. In effect, that's what an integer is.



The bitwise operators work as follows, & (AND, is used to "mask" or turn specific bits off), | (OR, is used to "include" or turn specific bits on), ^ (XOR is used to toggle: if they're on, they'll turn off, and vice versa).



So, you'd use int holder = 0; (to turn all of the switches off.)

And to turn on for example the third bit on you'd use

holder |= 4; (since 2^3/2 = 4) And to toggle it on and off holder ^= 4;



The highest number that holder will be is 63 (2^5 - 1). It helps if you know binary maths. To turn off the third bit, you'd use holder &= 59; (63 - 4).



I hope this helps, please email me if you'd like to know more.
?
2016-05-26 13:26:59 UTC
"s" is a char, not an int. You are trying to pass a char as an int without casting it in any way. So try public boolean isSpade(char suit) { boolean test; if(suit ='s') { test=true; } else {test=false; } return test; }
2010-01-12 10:59:13 UTC
Object[] i = new Boolean[8];



for (int j = 0; j < i.length; j++) {

i[j] = false;

System.out.println(i[j]);





}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...