Results 1 to 20 of 21
Thread: Boolean Arrays true/false switch
- 03-19-2013, 08:38 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 60
- Rep Power
- 0
Boolean Arrays true/false switch
I'm totally lost. I'm suppose to have make an Boolean Array which displays false originally and then alternate between true & false;
For example:
arr[0] = true;
arr[1] = false;
arr[2] = true;
Then I'm suppose to print in void printBoolean(boolean[] arr)
Can somebody veer me in the right direction?
Java Code:public class Boolean { void fillBoolean(boolean[] arr) { // fillBoolean will fill the arr array passed in with alternating true false values. boolean[] trueFalse = new boolean[] {true}; int n, i = n*2; for (n = 0; i < trueFalse.length; n++) { trueFalse[i] = !trueFalse[i]; } } void printBoolean(boolean[] arr){ // The printBoolean routine will print out the contents of the array arr passed in. int index; for (index = 0; index + 1 <= 20; index++) System.out.println(fillBoolean(arr)[index]); } public static void main(String[] args) { } }
- 03-19-2013, 09:51 PM #2
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Boolean Arrays true/false switch
1. Don't initialize your array in fillBoolean, the array is passed by the caller.
2. That's not how arrays are initialized ... Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
3. That link will also address why System.out.println(fillBoolean(arr)[index]); -> System.out.println(arr[index]));
4. I assume the array should be initialized in main then printed? then passed to fillBoolean, then (re-)printed ... if so void -> static void on both of your functions.
5. The class name Boolean is bad... Boolean already exists in java, and is kinda important.Last edited by SJF; 03-19-2013 at 09:52 PM. Reason: link not like
- 03-19-2013, 10:47 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 697
- Rep Power
- 1
- 03-19-2013, 11:44 PM #4
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Boolean Arrays true/false switch
Yeah, ok, so you might do that... if no. DON'T DO THAT.
boolean[] trueFalse = new boolean[] {true};
is a one member array.
not really useful and trueFalse[ANYTHING BUT 0] = no good.
boolean[] trueFalse = new boolean[] {false,false,false,false,false,false,false,false,f alse,false,false,false,false};
could work... but really?
- 03-19-2013, 11:57 PM #5
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Boolean Arrays true/false switch
But anyway Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics) default boolean = false,
so
Java Code:boolean[] bool = new boolean[400]; // is a new 400 element false array.
- 03-20-2013, 12:12 AM #6
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 697
- Rep Power
- 1
Re: Boolean Arrays true/false switch
I'm not certain of the point you are making. He initialized his array to true.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-20-2013, 12:16 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 697
- Rep Power
- 1
Re: Boolean Arrays true/false switch
The issue was not whether he was initializing to all false values or whether it was a 1 x1 array. Or whether his/her code is optimal. This issue was whether s/he could initialize that way. You said no. I said yes.
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 03-20-2013, 12:20 AM #8
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
- 03-20-2013, 01:20 AM #9
Member
- Join Date
- Feb 2013
- Posts
- 60
- Rep Power
- 0
Re: Boolean Arrays true/false switch
How do I get the value to appear? I.E. the output
arr[0] = true
arr[1] = false
arr[2] = true
Java Code:public class Boolean { void fillBoolean(boolean[] arr) { // fillBoolean will fill the arr array passed in with alternating true false values. int n = 1, i = n*2; for (i = 1; i < arr.length; i++) { arr[i] = !arr[i]; } } void printBoolean(boolean[] arr){ // The printBoolean routine will print out the contents of the array arr passed in. int index; for (index = 0; index + 1 <= 20; index++) System.out.println(arr[index]); } public static void main(String[] args) { } }
- 03-20-2013, 01:32 AM #10
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Boolean Arrays true/false switch
Well, your printBoolean() should do the trick. Create your array in the main function and call printBoolean(yourArray) (You'll have to declare printBoolean static first.)
- 03-20-2013, 01:54 AM #11
Member
- Join Date
- Feb 2013
- Posts
- 60
- Rep Power
- 0
Re: Boolean Arrays true/false switch
I'm still real new at this and haven't had much time to practice. How would I go about calling my printBoolean?
Java Code:public class Boolean { void fillBoolean(boolean[] arr) { // fillBoolean will fill the arr array passed in with alternating true false values. int n = 1, i = n*2; for (n = 1; i < arr.length; n++) { arr[i] = !arr[i]; } } static void printBoolean(boolean[] arr) { // The printBoolean routine will print out the contents of the array arr passed in. int index; for (index = 0; index + 1 <= 20; index++) System.out.println(arr[index]); System.out.println(arr); } public static void main(String[] args) { boolean[] arr = new boolean[] {true}; }
- 03-20-2013, 01:58 AM #12
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Boolean Arrays true/false switch
Java Code:printBoolean(arr);
- 03-20-2013, 02:04 AM #13
Member
- Join Date
- Feb 2013
- Posts
- 60
- Rep Power
- 0
Re: Boolean Arrays true/false switch
I'm getting this error.
it has something to do withException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Boolean.printBoolean(Boolean.java:21)
at Boolean.main(Boolean.java:28)
true
Java Code:static void printBoolean(boolean[] arr) { // The printBoolean routine will print out the contents of the array arr passed in. int index; for (index = 0; index + 1 <= 20; index++) System.out.println(arr[index]); }Java Code:public class Boolean { void fillBoolean(boolean[] arr) { // fillBoolean will fill the arr array passed in with alternating true false values. int n = 1, i = n*2; for (n = 1; i < arr.length; n++) { arr[i] = !arr[i]; } } static void printBoolean(boolean[] arr) { // The printBoolean routine will print out the contents of the array arr passed in. int index; for (index = 0; index + 1 <= 20; index++) System.out.println(arr[index]); } public static void main(String[] args) { boolean[] arr = new boolean[] {true}; printBoolean(arr); } }
- 03-20-2013, 02:17 AM #14
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Boolean Arrays true/false switch
Your for loop in printBoolean is looking for a boolean array of length 20. instead of (index = 0; index + 1 <= 20 ..... -> (index = 0; index < arr.length; index++) just like the for loop in fillBoolean.
Speaking of which .. (n=1; i < arr.length; n++) I don't think that's what you want... (but the i < arr.length is probably right, just not the n business)
- 03-20-2013, 02:20 AM #15
Member
- Join Date
- Feb 2013
- Posts
- 60
- Rep Power
- 0
- 03-20-2013, 02:28 AM #16
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Boolean Arrays true/false switch
If you get your OOB Exception to go away. (i < array.length is your friend). Your printBoolean will work.
Now then: when boolean (primitives) are initialized they are false by default. Enter fillBoolean(). So... every other entry should be false... a[i] = !a[i-1] ? Maybe? Note: array[-1] will also get you an OOB Exception.
- 03-20-2013, 02:33 AM #17
Member
- Join Date
- Feb 2013
- Posts
- 60
- Rep Power
- 0
- 03-20-2013, 02:36 AM #18
Member
- Join Date
- Feb 2013
- Posts
- 1
- Rep Power
- 0
Re: Boolean Arrays true/false switch
I've got to do this same problem. It's very confusing.
- 03-20-2013, 02:40 AM #19
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Boolean Arrays true/false switch
Look: There are two ways to get what you want. (Well more than that, but here are two)
First:
1. Set first item in array to true -> arr[0] = true;
2. Use a for loop (from 1 to 9) and set each array item to NOT it's predecessor -> arr[i] = !arr[i-1]
Second:
1. Go through the loop from 0 to 9 and set each even item true (they are false to begin with). if( i % 2 == 0)
.... or go through loop from 0 to 8 ; i+=2 setting arr[i] to true....
But steps in main are
A. initialize array.
B. call fillBoolean(yourArray)
C. call printBoolean(yourArray)
- 03-20-2013, 09:36 AM #20
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
Re: Boolean Arrays true/false switch
check your for loop
this got issue.Java Code:for (index = 0; index + 1 <= 20; index++) System.out.println(arr[index]); }
Refer this link:ttp://stackoverflow.com/questions/4958235/why-does-arrayindexoutofboundsexception-occur-and-how-to-avoid-it-in-android
Finally you put correct value for array index rangeRegards
Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475
Similar Threads
-
Return True for all Positive intgers in an Array, false otherwise
By Veronica91 in forum New To JavaReplies: 3Last Post: 09-09-2012, 04:35 PM -
Boolean.True and Boolean.False, why do some people use these?
By Pojahn_M in forum New To JavaReplies: 3Last Post: 09-13-2011, 12:01 AM -
getting a set Visible(true) turned false with an actionhandler.
By Mokomi in forum New To JavaReplies: 2Last Post: 05-25-2011, 02:33 PM -
how to balance true and false instances per id ?
By aneuryzma in forum New To JavaReplies: 1Last Post: 03-27-2011, 02:35 PM -
Prime Number - true , false
By pinkdreammsss in forum Java AppletsReplies: 11Last Post: 05-04-2010, 02:49 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks