Need help to explain seats at a cinema program made in java -
can please me understand going on here? find hard understand whats going on these multidimentional arrays. if explain me in detail program does. i'm supposed find first available seat @ movie theatre. program terminate first seat.
public class cinema { private boolean[][] seats = {{ true, true, false, false, false, true, true false}, { true, true, true, true, true, true, true, true}, { true, true, true, true, true, true, true, true}} public void findavailable() { boolean found = false; int row = 0; while (!found && row < seats.length) { int seat = 0; while (!found && seat < seats[row].length) { if (seats[row][seat]) { found = true; system.out.println("row = " + row + " seat = " + seat); } seat++; } row++; } }
a movie theater way explain 2 dimensional array.
boolean[][] seats = {{true, true, false, false, false, true, true false}, {true, true, true, true, true, true, true, true}, {true, true, true, true, true, true, true, true} }
you see every row in array row of seats. can use first seat seats[0][0]
, or second seat on first row seats[0][1]
. in order go through seats on first row, can make loop:
for(x = 0; x < 8; x++){ system.out.println("the boolean value of seat " + x + " on first row is: " + seats[0][x]); }
now can search second row (keep in mind index of array starts @ 0) seats[1][x]
now, if loop through possible seats, have make 2 loops: 1 loop through number of seat , 1 loop through rows:
for(y = 0; y < 3; y++){ for(x = 0; x < 8; x++){ system.out.println("the boolean value of seat " + x + " on row "+ y + " is: " + seats[y][x]); } }
note cannot loop further size of array (3 rows , 8 seats). why using .length
attribute determine size.
now thing find first seat available, loop through array once again , when boolean value @ particular seat true
, have break out of both of loops. why there boolean variable found
, set true
when seat found , causes loop not execute again (note !
in while condition).
Comments
Post a Comment