programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# 2-D array of boolean

Greenhorn
Posts: 18
as a boolean variable can have only two values-true,false..so why exactly do we need an array of boolean values.???

Ranch Hand
Posts: 637
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
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...

Java Cowboy
Sheriff
Posts: 16083
88

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.

lowercase baba
Bartender
Posts: 12624
50
• 1
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.