1

I'm a beginner when it comes to ArrayLists, so i'm sort of confused how I would be able to turn a 2D array into a ArrayList. I'm currently writing a program that reserves theatre seats, but i'm using a 2D array for the Rows and the seats in the row, but I want to use an ArrayList instead of the 2D array. I can't find anything else on this whenever I try to search for it. I have 2 other classes which are the getters and setters for the selling and reserving of the tickets, and a main class for the menu and switch statement. Thanks!

import java.util.Scanner;

/**

*/
public class Theatre {

//Number of rows in the theatre
public static final int NUMBER_ROWS = 10;
//Number of seats that are in each row
public static final int NUMBER_OF_SEATS_IN_ROW = 15;
private Seat[][] seat = new Seat[NUMBER_ROWS][NUMBER_OF_SEATS_IN_ROW];

public Theatre(){
    for(int x=0;x<seat.length;x++){
        for(int y=0;y<seat[x].length;y++){
            seat[x][y] = new Seat();

            if(x<5){ // If row is less than 5, set price of seat to 100
                seat[x][y].setPrice(100);
            }else{ // If row is not less than 5, set price to 70
                seat[x][y].setPrice(70);
            }
        }
    }
}


/**
 * This method prompts for row and seat number and reserves it under a name if it is not already reserved
 */
public void reserveSeat(){
    Scanner input = new Scanner(System.in);
    int row;
    int seat;
    //Gathering row number with validation
    do{
        System.out.print("Please select row: ");
        row = input.nextInt();
        row--;
    }while(row<0||row>=NUMBER_ROWS);
    //Gathering seat number with validation
    do{
        System.out.print("Please select seat: ");
        seat = input.nextInt();
        seat--;
    }while(seat<0||seat>=NUMBER_OF_SEATS_IN_ROW);
    if(row<5){
        System.out.println("Price: $100");
    }else{
        System.out.println("Price: $70");
    }

    if(this.seat[row][seat].isSold()){ // If seat is reserved, display message
        System.out.println("This seat is reserved");
    }else{ // If seat is not reserved, prompt for name and reserve it

        System.out.print("Please enter your name to reserve seat: ");
        this.seat[row][seat].setReservedBy(input.next());
        this.seat[row][seat].setSold(true);
    }
}
/**
 * This method displays all the seats and gives a visual representation of which seats are reserved
 */
public void showReservations(){
    String output = "";
    for(int x=0;x<seat.length;x++){
        for(int y=0;y<seat[x].length;y++){
            if(seat[x][y].isSold()){ // If seat is sold, append "x"
                output += "x ";
            }else{ // If seat is not sold, append "o"
                output += "o ";
            }
        }
        output += "Row "+(x+1)+"\n"; // Append newline character when row is complete
    }
    System.out.println(output);
}

/**
 * This method calculates the total value of seats sold and displays it
 */
public void showTotalValue(){
    double totalValue = 0;
    for(int x=0;x<seat.length;x++){
        for(int y=0;y<seat[x].length;y++){
            if(seat[x][y].isSold()){ // If seat is sold, add price of seat to totalValue
                totalValue += seat[x][y].getPrice();
            }
        }
    }
    System.out.println("The total value of seats sold is $"+totalValue);
}

}

Shuckyducky
  • 71
  • 11

2 Answers2

0

You create an ArrayList<ArrayList<Seat>> like so:

public class Theatre {
    //Number of rows in the theatre
    public static final int NUMBER_ROWS = 10;
    //Number of seats that are in each row
    public static final int NUMBER_OF_SEATS_IN_ROW = 15;
    // Passing NUMBER_ROWS to the constructor sets aside that much space
    // but the array is still empty.
    private ArrayList<ArrayList<Seat>> seat = new ArrayList<ArrayList<Seat>>(NUMBER_ROWS);

    public Theatre(){
        for(int x=0; x NUMBER_ROWS; x++){
            seat.add(new ArrayList<Seat>(NUM_OF_SEATS_IN_ROW);
            for(int y=0; y < NUMBER_OF_SEATS_IN_ROW; y++){
                seat.get(x).add(new Seat());
                // etc.
            }
         }
    }
}
Oliver Dain
  • 9,617
  • 3
  • 35
  • 48
  • Change `private List> seat` `private List –  Feb 26 '17 at 01:19
  • It has to be `new ArrayList>(NUMBER_ROWS)`. The types won't match otherwise. – Calculator Feb 26 '17 at 01:20
  • How would you call a method from another class with the ArrayList? If you look in my code it sets a price to the specific seat if "x" is lower then 5. – Shuckyducky Feb 26 '17 at 01:21
  • @Shuckyducky are you talking about this line: `seat[x][y].setPrice(...)`? Just change to `seat.get(x).get(y).setPrice(...)`. As for the types, yes, they don't match (I didn't actually try it). Will fix. – Oliver Dain Feb 26 '17 at 01:58
  • How would it work for the isSold() part of the code under the reserveSeat method? – Shuckyducky Feb 26 '17 at 02:17
0

You may want to build a list of lists, like so:

List<List<Seat>> seats = new ArrayList<List<Seat>>();

You could then add more rows like so:

seats.add(new ArrayList<Seat>);

And get the seat object like so:

seats.get(row).get(column);

You also might be able to find more information here: 2 dimensional array list

Community
  • 1
  • 1