如何在Java中将2D数组更改为ArrayList
我是ArrayList的初学者,所以我有点困惑如何将2D数组转换为ArrayList。我目前正在编写一个保留剧院座位的程序,但我对行和行中的座位使用2D数组,但我想使用ArrayList而不是2D数组。每当我试图搜索它时,我都找不到其他任何东西。我还有另外两门课,一门是售票和订票的获取者和设定者,另一门是菜单和切换语句的主课。谢谢如何在Java中将2D数组更改为ArrayList,java,arrays,arraylist,Java,Arrays,Arraylist,我是ArrayList的初学者,所以我有点困惑如何将2D数组转换为ArrayList。我目前正在编写一个保留剧院座位的程序,但我对行和行中的座位使用2D数组,但我想使用ArrayList而不是2D数组。每当我试图搜索它时,我都找不到其他任何东西。我还有另外两门课,一门是售票和订票的获取者和设定者,另一门是菜单和切换语句的主课。谢谢 import java.util.Scanner; /** */ public class Theatre { //Number of rows in the
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);
}
import java.util.Scanner;
/**
*/
公营剧院{
//剧院的排数
公共静态最终整数行数=10;
//每排的座位数
公共静态最终整数排座位数=15;
私人座位[]座位=新座位[排数][排数];
公共剧场(){
对于(int x=0;x您可以创建一个ArrayList
,如下所示:
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.
}
}
}
}
公共级剧院{
//剧院的排数
公共静态最终整数行数=10;
//每排的座位数
公共静态最终整数排座位数=15;
//将NUMBER_行传递给构造函数会留出那么多空间
//但是数组仍然是空的。
专用ArrayList席位=新ArrayList(行数);
公共剧场(){
对于(int x=0;x行数;x++){
seat.add(新数组列表(行中的SEATS的数量);
对于(int y=0;y<第二排座位的数量;y++){
seat.get(x).添加(新seat());
//等等。
}
}
}
}
您可以创建一个数组列表,如下所示:
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.
}
}
}
}
公共级剧院{
//剧院的排数
公共静态最终整数行数=10;
//每排的座位数
公共静态最终整数排座位数=15;
//将NUMBER_行传递给构造函数会留出那么多空间
//但是数组仍然是空的。
专用ArrayList席位=新ArrayList(行数);
公共剧场(){
对于(int x=0;x行数;x++){
seat.add(新数组列表(行中的SEATS的数量);
对于(int y=0;y<第二排座位的数量;y++){
seat.get(x).添加(新seat());
//等等。
}
}
}
}
您可能需要建立一个列表列表,如下所示:
List<List<Seat>> seats = new ArrayList<List<Seat>>();
您还可以在此处找到更多信息:您可能需要构建一个列表列表,如下所示:
List<List<Seat>> seats = new ArrayList<List<Seat>>();
您还可以在此处找到更多信息:ArrayList是一个线性集合。转换与每行和每列相关的2D数组将不再与ArrayList相关。是否有必要将ArrayList与此程序一起使用?我不知道ArrayList不会与列和行相关,除非我正在阅读此说明g、 您需要一个ArrayList of ArrayList。2D数组可以转换为:List lst=new ArrayList()
@shuckydacky要关联列和行,您需要ArrayList的ArrayList,这不是最适合您的情况。ArrayList是一个线性集合。转换与每一行和列相关的2D数组将不再与ArrayList相关。是否仍要将ArrayList与此程序一起使用?我不知道ArrayList不会关联列和行,除非我读错了。您需要一个ArrayList的ArrayList。2D数组可以转换为:List lst=new ArrayList()
@shuckydacky要关联列和行,您将需要ArrayList的ArrayLists,但这并不是最适合您的情况。更改私有列表位置
私有列表必须是新的ArrayList(行数)
。否则类型将不匹配。您如何使用ArrayList从另一个类调用方法?如果您查看我的代码,如果“x”低于5,它会为特定的座位设置一个价格。@shckyducky您正在谈论这一行:seat[x][y]。setPrice(…)
?只需更改为seat.get(x).get(y).setPrice(…)
。至于类型,是的,它们不匹配(我实际上没有尝试)。将修复。在reserveSeat方法下,它对代码的isSold()部分如何工作?更改私有列表席位私有列表它必须是新的ArrayList(行数)
。否则类型将不匹配。您如何使用ArrayList从另一个类调用方法?如果您查看我的代码,如果“x”低于5,它会为特定的座位设置一个价格。@shckyducky您正在谈论这一行:seat[x][y]。setPrice(…)
?只需更改为seat.get(x).get(y).setPrice(…)
。至于类型,是的,它们不匹配(我实际上没有尝试)。将进行修复。在reserveSeat方法下,代码的isSold()部分如何工作?