【问题标题】:How to change a 2D Array into an ArrayList in Java如何在 Java 中将 2D 数组更改为 ArrayList
【发布时间】:2017-02-26 01:09:32
【问题描述】:

我是 ArrayLists 的初学者,所以我有点困惑如何将 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);
}

}

【问题讨论】:

  • ArrayList 是一个线性集合。转换每行和列相关的二维数组将不再与 ArrayList 相关。
  • 有没有办法在这个程序中使用 ArrayList?我不知道 ArrayList 不会关联列和行,除非我读错了。
  • 你需要一个 ArrayList 的 ArrayList。
  • 一个二维数组可以转换成:List&lt;ArrayList&lt;Seat&gt;&gt; lst = new ArrayList&lt;ArrayList&lt;Seat&gt;&gt; ();
  • @Shuckyducky 将您需要的 ArrayLists 的列和行关联起来,这对您的情况不是最好的。

标签: java arrays arraylist


【解决方案1】:

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

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

然后您可以像这样添加更多行:

seats.add(new ArrayList<Seat>);

然后像这样得到座位对象:

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

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

【讨论】:

    【解决方案2】:

    您像这样创建ArrayList&lt;ArrayList&lt;Seat&gt;&gt;

    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.
                }
             }
        }
    }
    

    【讨论】:

    • 更改private List&lt;List&lt;Seat&gt;&gt; seat private List&lt;ArrayList...
    • 必须是new ArrayList&lt;List&lt;Seat&gt;&gt;(NUMBER_ROWS)。否则类型将不匹配。
    • 如何使用 ArrayList 调用另一个类的方法?如果您查看我的代码,如果“x”低于 5,它会为特定座位设置价格。
    • @Shuckyducky 你在说这条线:seat[x][y].setPrice(...)?只需更改为seat.get(x).get(y).setPrice(...)。至于类型,是的,它们不匹配(我实际上没有尝试过)。会修复的。
    • reserveSeat 方法下代码的 isSold() 部分如何工作?
    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 2017-05-31
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多