-1

I met a problem when I want to assign the parking to each order, and then remove the parking-assined order from other parkings' order sets. This array list arr_assignedOrderSet has a size of 22, so it was supposed to iterate 22 times. But I have no idea why it stoped after 11 times. If I replace the iterated times arr_assignedOrderSet.size() with 22, it will show me an error of "java.lang.IndexOutOfBoundsException: Index 11 out of bounds for length 11". The codes are as following:

ArrayList<Order> arr_assignedOrderSet = new ArrayList<Order>();
arr_assignedOrderSet = pop_parkings.get(index).orderSet;

for(int i=0; i<arr_assignedOrderSet.size(); i++){
    Order order = arr_assignedOrderSet.get(i);
    for(int j=0; j<order.col_parking.size(); j++){
        Parking p = order.col_parking.get(j);
        p.orderSet.remove(order);
    }

}

Thanks a lot for your help~ ;-)

Yu LIU
  • 21
  • 2
  • How do you create the `pop_orders` agents? At the beginning you create 22 of them? – Yashar Ahmadov Nov 16 '21 at 11:35
  • I created 100 orders and first assigned the parking to 22 orders of them. – Yu LIU Nov 16 '21 at 11:38
  • And when do you run those loops? At the start? – Yashar Ahmadov Nov 16 '21 at 11:40
  • 1
    You say "There are 22 orders in arr_assignedOrderSet, like this:" but the code clearly shows only 11 :) – Benjamin Nov 16 '21 at 11:43
  • Yes, you are right!! When I commented the loop of "order.col_parking" and just print out the loop of "arr_assignedOrderSet.size()", I can get 22 orders. But I don't know why if I put the loop back and the result was halved... – Yu LIU Nov 16 '21 at 11:50
  • Hello Benhamin, there are definitely 22 orders in the arr_assignedOrderSet. And when I printed it out, it works well. But when I need to nest with another loop, it can only iterate half times. I am not sure if you can get my problem, or I need to add more codes and explanation. Thanks a lot~ – Yu LIU Nov 16 '21 at 12:11

1 Answers1

0

Without fully understanding your model, it seems suspicious that 11 is exactly one half of 22, which would mean that this statement: p.orderSet.remove(order); is shrinking the same orderSet being iterated in pop_parkings.get(index).orderSet via arr_assignedOrderSet variable. When this assignment happens arr_assignedOrderSet = pop_parkings.get(index).orderSet; Java isn't actually copying the content of the collection, only the reference to it. If that is the case then the fix is to replace this line:

arr_assignedOrderSet = pop_parkings.get(index).orderSet;

with this line:

arr_assignedOrderSet.addAll(pop_parkings.get(index).orderSet);

Artem P.
  • 816
  • 1
  • 6
  • 8
  • Hello Artem, thanks for your answer. When I print out the result of this array list, everything works well, as shown here, "There are 22 orders in arr_assignedOrderSet, like this:". Which means after changing the code, I still get the same result. :( Thanks you anyway. – Yu LIU Nov 16 '21 at 12:08
  • You could be printing it out before you start removing elements so it looks fine at the printing time but not at the runtime. Do you know which line causes the index out of bounds error? – Artem P. Nov 16 '21 at 16:32
  • Hello Artem, thanks a lot for your suggestion. I've found that the error is with this line: Order order = arr_assignedOrderSet.get(i); And the problem was tackled when I change it into Order order = arr_assignedOrderSet.get(0); to get each element until the array become empty. – Yu LIU Nov 30 '21 at 16:02