Imagine you are writing a program to sell tickets for a theater, tickets are priced based locations. There are two levels: orchestra and grand tier balcony. How would you store the ticket availability and price info?
In Java, arrays can be multiple dimensional. In the above example, we can create a 2-D array to store price info for one level, then create a 3-D array to store the price info for two levels.
Similar applications of 2-D arrays can be found in these applications: parking lots, board games, maps, pixels etc.
A 2-D array is an array of arrays, i.e. each row is an array itself. The number of rows can be any integer, and the number of columns in each row can be different.
Mostly, we use rectangle arrays where the length of the 2-D array is the number of rows, and the length of any row is the number of columns.
A nested for loop can be used to traverse a 2-D array.
Declare and initialize a boolean 2-D array to store ticket availability for orchestra level. Assuming blue seats are all sold out.
Declare and initialize an int 2-D array to store three seat price levels(Value, Standard, Premium) for orchestra level.
Declare and initialize a double 2-D array to each seat prices for orchestra level. Use -1 to indicate "not available". Assuming no seats have been sold yet, and Standard = $55.50, Value = $39.50, Premium = $99.50, initialize the array.
Store the multiplication table below in a 2-D array.
R7.29 and R7.32 on this page
Use the array in B#3 to sell tickets at each price level. Ask users how many tickets they want to buy and which row and column each seat is at. Take input from the keyboard, then check the availability. If the requested seats are sold, ask the user to select again. Once all requested seats are available, print out the tickets and the total amount due, then mark those seats as sold to complete this transaction. You can start with a small 3x3 array and expand as your code develops.