【发布时间】:2020-12-02 16:43:55
【问题描述】:
我正在向 Udemy 学习 Java,导师编写了以下代码...
这里的theatre.seat 是一个Seat 对象的arrayList,它是Theatre 类的内部类。 seatCopy 是通过在其构造函数中传递 theatre.seats 获得的 theatre.seats 的副本。 pritnList 仅用于在控制台上打印列表。
List<Theatre.Seat> seatCopy = new ArrayList<>(theatre.seats);
Collections.shuffle(seatCopy);
System.out.println("Printing seat copy : ");
printList(seatCopy);
System.out.println("Printing theatre.seat : ");
printList(theatre.seats);
由于这是一个浅拷贝,即 seatCopy 和 theatre.seats 都引用同一个 arrayList 对象,那么当在 seatCopy 上调用 shuffle 方法时,不应该以相同的方式对两个列表进行混洗和打印吗?
以下是输出:
seatCopy 被洗牌,但 theatre.seats 不是。为什么?
【问题讨论】:
-
请不要发布图片。以可读的方式在您的问题中提供输出。
-
对不起!我会注意以后的问题
标签: java arraylist collections