2017-02-25 165 views
-1

需要爲行數創建一個數組列表,並且我對如何執行該操作有點困惑。將數組更改爲數組列表

這是我的代碼。

import java.util.ArrayList; 
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.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(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); 
} 
} 

回答

0

可以定義多維ArrayList這樣 ArrayList<ArrayList<Integer>> array = new ArrayList<ArrayList<Integer>>(); 並使用三種方法array.add(input value), array.get(index), array.set(index, value)添加新的號碼,從數組或更新值獲得價值。

例如:

package test; 

import java.util.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; 
    ArrayList<ArrayList<Integer>> seat = new ArrayList<ArrayList<Integer>>(); 

    public Theatre(){ 
     for(int x=0; x<NUMBER_ROWS;x++){ 
      ArrayList<Integer> arrSeat = new ArrayList<Integer>(); 
      for(int y=0;y<NUMBER_OF_SEATS_IN_ROW; y++) { 
       if(x<5){ // If row is less than 5, set price of seat to 100 
        arrSeat.add(100); 
       }else{ // If row is not less than 5, set price to 70 
        arrSeat.add(70); 
       } 
      } 
      seat.add(arrSeat); 
     } 
    } 

    /** 
    * 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<NUMBER_ROWS;x++){ 
      for(int y=0;y<NUMBER_OF_SEATS_IN_ROW;y++){ 
       if(x==2 && y==3 || x==5&&y==2 || x==4&&y==4) { 
        seat.get(x).set(y, 0); 
       } 
       if(seat.get(x).get(y) == 0) { 
        output += " x "; 
       } else { 
        output += " o "; 
       } 
      } 
      output += "Row "+(x+1)+"\n"; // Append newline character when row is complete 
      } 
      System.out.println(output); 
     } 

     public static void main(String[] args) { 
      Theatre theatre = new Theatre(); 
      theatre.showReservations(); 
    } 
} 
0

你在題目中提出的問題是「如何將數組轉換爲數組列表」。下面是將數組轉換爲數組列表並返回到數組的示例。假設您已經設置了tagArray。

//get String tagArray 
    String[] tagArray = getTagArray(); 

    //convert to array list 
    List<String> tempArrayList = new ArrayList<>(Arrays.asList(tagArray)); 

    //convert back to string array 
    String[] tagArray2 = new String[tempArrayList.size()]; 
    for (int i=0; i<tempArrayList.size(); i++) teagArray2[i] =tempArrayList.get(i);