0

I am trying to move one class from an arraylist to another of the same type but associated with a different class. The reason im doing this is that the vehicle is moving from one class (Depot) to another. And I want to replicate that. However I'm finding it difficult to do so.

Here is my code:

if (depotSelection.equals("Lpool")) {
    Vehicle v = depots.get(depotNo).getVehiclebyReg(vehicleSelection);
    depots.get(0).makeVehicle(v);
    depots.get(depotNo).removeByVehicleReg(vehicleSelection);
    exit = true;
    break;
}
if (depotSelection.equals("Leeds")) {
Vehicle v = depots.get(depotNo).getVehiclebyReg(vehicleSelection);
depots.get(1).makeVehicle(v);
depots.get(depotNo).removeByVehicleReg(vehicleSelection);
exit = true;
break;
}
if (depotSelection.equals("MChester")) {
    Vehicle v = depots.get(depotNo).getVehiclebyReg(vehicleSelection);
    depots.get(2).makeVehicle(v);
    depots.get(depotNo).removeByVehicleReg(vehicleSelection);
    exit = true;
    break;
}

And here is the code to that I am using to remove it:

public void removeByVehicleReg(String regNo) {
    vehicles.removeIf(v -> v.getRegNo().equals(regNo));
}

This is the current exception I am getting:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042)
    at java.base/java.util.ArrayList$Itr.next(ArrayList.java:996)
    at DepotSystem.reAssignVehicles(DepotSystem.java:400)
    at DepotSystem.depotMenu(DepotSystem.java:166)
    at DepotSystem.logOn(DepotSystem.java:123)
    at DepotSystem.entryMenu(DepotSystem.java:89)
    at Entry.main(Entry.java:10)
khelwood
  • 55,782
  • 14
  • 81
  • 108
David
  • 21
  • 6

0 Answers0