0

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);

                    }
                }
            }
        }

    }
}

1 Answers1

0

The problem with your code is not with remove() but lies in the last if clause

if (w.current_weight <= w.max_weight) {

if this is false, that is there is not enough space in the current wagon for the current cargo, then this not only means that the cargo will not be added to the wagon but also that this particular cargo will not be part of ny wagon at all since the end of the loop will be reached and the next cargo will be fetched instead using hasNext().

At this point I assume it would easily happen to more cargos that they will not fit in the current wagon since it is most likely quite full. Once the end of the stIterator has been reached you get a new wagon to fill using previous() but now stIterator.hasNext() will always return false since your at the end of that iterator so no more wagons will be filled with any cargo.

There are several ways to solve this but one thing I think you should do is to separate responsibilities and let the Wagon class take responsibility for adding a cargo, this will make it simpler to write a working loop in my opinion.

I would suggest an add method in the Wagon class

public boolean add(Cargo cargo) {
  if ((cargo.weight > max_weight) || (current_weight + cargo.weight > max_weight)) {
    return false; 
  }

  current_weight += cargo.weight
  cargos.add(cargo);

  return true;
}
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52