Yu LIU
Yu LIU

Reputation: 21

How to solve "java.lang.IndexOutOfBoundsException" problem via Agent-based modeling in Anylogic?

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

Upvotes: -1

Views: 931

Answers (1)

Artem P.
Artem P.

Reputation: 816

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

Upvotes: 0

Related Questions