Okay so i have two ArrayLists. The first arraylist starts from the end and tries to add items from my second, which starts from the beginning. I have to listIterators and the concept is to add items to a list of the first one until a maximum capacity is reached, then i continue the additions of new items to the second item of the first list and so on. The problem is when i try to remove an item from the second list, the list won't be reordered and some items won't be checked. Here's the code:
public void loadFromBeh(ArrayList<Cargo> storage) {
ListIterator<Wagon> waIterator = wagons.listIterator(wagons.size());
ListIterator<Cargo> stIterator = storage.listIterator();
while (waIterator.hasPrevious()) {
Wagon w = waIterator.previous();
while (stIterator.hasNext()) {
Cargo c = stIterator.next();
System.out.println("pr " + w.current_weight);
if (c.weight <= w.max_weight) {
if (w.current_weight == 0) {
w.current_weight = c.weight;
System.out.println("first " + w.current_weight);
stIterator.remove();
} else {
w.current_weight = w.current_weight + c.weight;
System.out.println("new current " + w.current_weight);
if (w.current_weight <= w.max_weight) {
w.cargos.add(c);
stIterator.remove();
System.out.println("ok " + w.current_weight);
}
}
}
}
}
}