java - 如何在 Java 中将 2D Array 更改为 ArrayList

标签 java arrays arraylist

我是 ArrayList 的初学者,所以我有点困惑如何将 2D 数组转换为 ArrayList。我目前正在编写一个保留剧院座位的程序,但我使用 2D 数组作为行和行中的座位,但我想使用 ArrayList 而不是 2D 数组。每当我尝试搜索它时,我都找不到任何其他内容。我还有另外 2 个类,它们是用于门票销售和预订的 getter 和 setter,以及用于菜单和 switch 语句的主类。谢谢!

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);
}

}

最佳答案

您可能想要构建一个列表列表,如下所示:

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

然后您可以添加更多行,如下所示:

seats.add(new ArrayList<Seat>);

并像这样获取座位对象:

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

您还可以在这里找到更多信息:2 dimensional array list

关于java - 如何在 Java 中将 2D Array 更改为 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42463577/

相关文章:

java - ArrayList 中的通用集合

java - 为什么输出会有差异?

java - 迭代循环比较当前索引与下一个索引java

javascript - 如何存储数组中所有小于前 3 个值的值?

javascript - 如何在 javascript 中创建动态函数?

java - 需要帮助从 ArrayList 中删除由扫描仪添加的项目

java - 如何使用JavaCL创建NVIDIA CUDA的CLContext?

java - 多线程环境下ResultSet成员变量的复用

c - 调试散列结果 : Lengths, 数组、字符

java - 我尝试使用处理(Java)中的旋转和投影矩阵以 2D 形式渲染 3D 对象(立方体)