在java中更改2d arraylist中的一行会更改所有行

r7knjye2  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(381)

我想根据输入在电影院大厅预定一个座位,但当我试图这样做时,它改变了所有的排。
预订的座位用“b”表示。
更改电影院大厅状态的方法:

public void bookSeat(int row, int seat) {
        this.seatsArrangement.get(row  - 1).set(seat - 1, 'B');
    }

输入和输出:

Enter the number of rows:
> 7
Enter the number of seats in each row:
> 8
Cinema:
  1 2 3 4 5 6 7 8 
1 S S S S S S S S 
2 S S S S S S S S 
3 S S S S S S S S 
4 S S S S S S S S 
5 S S S S S S S S 
6 S S S S S S S S 
7 S S S S S S S S 
Enter a row number:
> 2
Enter a seat number in that row:
> 4
Cinema:
  1 2 3 4 5 6 7 8 
1 S S S B S S S S 
2 S S S B S S S S 
3 S S S B S S S S 
4 S S S B S S S S 
5 S S S B S S S S 
6 S S S B S S S S 
7 S S S B S S S S

完整代码:

package cinema;
import java.util.*;
public class Cinema {

    private final int rows;
    private final int seats;
    private final List<List<Character>> seatsArrangement = new ArrayList<>();

    public Cinema (int rows, int seats) {
        this.rows = rows;
        this.seats = seats;
        List<Character> rowArrangement = new ArrayList<>();
        while (seats-- != 0) {
            rowArrangement.add('S');
        }
        while (rows-- != 0) {
            seatsArrangement.add(rowArrangement);
        }
    }

    public int calculateProfit() {
        if (this.rows * this.seats <= 60) {
            return this.rows * this.seats * 10;
        } else {
            return (int) ((Math.floor(this.rows / 2.0) * 10 + (this.rows - Math.floor(this.rows / 2.0)) * 8) * this.seats);
        }
    }

    public void showSeatsArrangement() {
        System.out.print("Cinema:\n  ");
        int i = 1;
        while (i <= this.seats) {
            System.out.printf("%d ", i++);
        }
        i = 1;
        for (var row : this.seatsArrangement) {
            System.out.print("\n" + i + " ");
            for (var seat : row) {
                System.out.printf("%c ", seat);
            }
            ++i;
        }
    }

    public void bookSeat(int row, int seat) {
        this.seatsArrangement.get(row  - 1).set(seat - 1, 'B');
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number of rows:");
        int rows = sc.nextInt();
        System.out.println("Enter the number of seats in each row:");
        int seats = sc.nextInt();

        Cinema cinema = new Cinema(rows, seats);
        cinema.showSeatsArrangement();

        System.out.println("\nEnter a row number:");
        int row = sc.nextInt();
        System.out.println("Enter a seat number in that row:");
        int seat = sc.nextInt();

        cinema.bookSeat(row, seat);
        cinema.showSeatsArrangement();
    }
}
yzckvree

yzckvree1#

seatsarangement数组的每个元素都是相同的arraylist。您正在将相同的arraylist rowarrangement添加到SeatArrangement。若要解决此问题,请在第二个while循环中创建rowarrangement的副本。例如

while (rows-- != 0) {
            List<Integer> tmp = new ArrayList<>(rowArrangement);
            seatsArrangement.add(tmp);
        }
vuv7lop3

vuv7lop32#

谢谢@mci对你的帮助这是个愚蠢的错误。
在构造函数中做了一些更改:
原始版本:

public Cinema (int rows, int seats) {
        this.rows = rows;
        this.seats = seats;
        List<Character> rowArrangement = new ArrayList<>();
        while (seats-- != 0) {
            rowArrangement.add('S');
        }
        while (rows-- != 0) {
            seatsArrangement.add(rowArrangement);
        }
    }

工作版本:

public Cinema(int rows, int seats) {
        this.rows = rows;
        this.seats = seats;
        List<Character> rowArrangement = new ArrayList<>();
        while (seats-- != 0) {
            rowArrangement.add('S');
        }
        while (rows-- != 0) {
            seatsArrangement.add(new ArrayList<>(rowArrangement));
        }
    }

但出于某种原因 rowArrangment.clone() 无法工作,获取此错误
“clone()”在“java.lang.object”中具有受保护的访问权限
这解决了问题,但我不知道为什么?
((arraylist)rowarrangement).clone();
编辑:我们需要显式强制转换rowarrangment的原因是 clone() 方法是函数的积分性质 ArrayList 自从我以前 List 所以我不能打电话 clone() 方法,因为向上转换不支持访问子类的方法。

相关问题