Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

2-D array of boolean  RSS feed

 
Subhendu Dash
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as a boolean variable can have only two values-true,false..so why exactly do we need an array of boolean values.???
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cooked this example up. I dont know if it will be of much use in the real-world. hth

There is a building of [3] floors , each having [4] rooms with just 1 bulb in each room. Bulb can be true/false. Now find out in which room light is on and tell that person to go to sleep.



output :



PS : The problem you mentioned doesnt really need an example. but it doesnt hurt to imagine. good luck and thanks for the practice i had due to you !
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make this a 3D problem, just add multiple bulbs to a room.

boolean b[3] is an array that CAN hold 3 references to boolean variables.

boolean b[3] [ ] is an array of 5 references to boolean arrays - its like : " b[3] {b1[], b2[],b2[]} " . The size of b1, b2, b3 can be different.
b[1] , b[2] , b[3] are a references to boolean arrays and NOT 3 booleans. b[1] [3] , that is a boolean. I guess this is where you have misunderstood.

boolean b[3] [ ] [ ] is like " b[3] {b1[ ] [ ], b2 [ ] [ ] , b3 [ ] [ ]} " etc...

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhendu Dash wrote:as a boolean variable can have only two values-true,false..so why exactly do we need an array of boolean values.???

Your question is a bit strange. The fact that a boolean can only be true or false does not have anything to do with being able to create an array of boolean values.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need an array (or better, you can USE an array) any time you need to remember more than one instance of a variable. I may have an array of booleans to hold whether a group of students have paid their dues. I need to recall a yes or not values for EACH student.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!