Which Array To Use?
I am designing a new cinema booking system, where there will be 4 screens holding various number of seats on each.
I need to know what ARRAY or what data structure is best for ADDING and REMOVING people onto or off seats ANYWHERE they want on the screen.
I believe that I will n开发者_高级运维eed a 2 dimentional array structure, but any help will be much appreciated!... thank you
What you should do is build a class structure that will abstract out the way you are storing the data in memory. You might do something like:
class Cinema {
List<Auditorium> screens;
}
class Auditorium {
int number;
List<SeatRow> rows;
}
class SeatRow {
int rowNumber
List<Seat> seats;
}
class Seat {
int seatNumber;
boolean occupied;
}
Note: This isn't the only solution. You may want to look into storing Seats
in a Map
or some other data structure.
ArrayList is a nice simple array type list that permits adding and removing.
You could create an array of arrays if you like. That would be a 2 dimentional array.
Sounds like 4 instances of the same data structure. Possibly a Collection of Screen objects that contain a Collection of seats.
I'd vote personally for a plain and simple Seat[][]
array of the class structure suggested above. There's simply no reason to use another array as the array size probably isn't ever going to change, if it is that can be done at initialisation. Then just show them or don't on the screen by either data in a certain position or null
for unreserved seats.
Depending on your need (do you need to remember where the guest booked?) you could even use boolean[][]
as the bare minimum.
No matter what, just create and name four different such Seat 2D arrays. Preferably stick them in a separate class and have setters and getters.
精彩评论