我认为这些是算术。
我已经创建了3个这样的对象:
for (int j = 1; j < 4; j++) {
int parkingSlot= 1 + rd.nextInt(3);
AircraftCarrier ac= new AircraftCarrier (fc, j, parkingSlots, parkingSlots);
}
基于类AircraftCarrier(它的构造函数):
public AircraftCarrier (FlightControl fc, int idC, int parkingSlots, int freeParkingSlots) {
this.kontrolaLotow = fc;
this.id = idC;
this.ps = parkingSlots;
this.fps = freeParkingSlots;
}
我有3艘航空母舰,对吗?假设我需要为id = 2的运营商更改freeParkingSLots值。我怎么做?
最佳答案
您创建了三个实例,但是由于您没有维护对它们的引用,因此您不再拥有它们。它们已被发送到垃圾收集。
您需要将每个实例存储在某个集合中,以供以后访问。
List<AircraftCarrier> myList = new ArrayList<AircraftCarrier>();
for (int j = 1; j < 4; j++) {
int parkingSlot= 1 + rd.nextInt(3);
AircraftCarrier ac= new AircraftCarrier (fc, j, parkingSlots, parkingSlots);
myList.add(ac);
}