|
From: Beyeler, W. E <we...@sa...> - 2007-07-09 21:05:49
|
My application makes heavy use of the Schedule.removeAction method. I
am regularly getting an array index out of bounds exception due to an
attempt to get at a missing array element:
Exception in thread "Thread-2" java.lang.IndexOutOfBoundsException:
Index: 1, Size: 1
at java.util.ArrayList.RangeCheck(ArrayList.java:546)
at java.util.ArrayList.get(ArrayList.java:321)
at uchicago.src.sim.engine.ScheduleGroup.execute(Unknown Source)
at uchicago.src.sim.engine.RandomScheduleGroup.execute(Unknown
Source)
at uchicago.src.sim.engine.Schedule.execute(Unknown Source)
at uchicago.src.sim.engine.BaseController$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:595)
Looking at the code for ScheduleGroup.execute
public void execute() {
for (int i =3D 0, n =3D actions.size(); i < n; i++) {
((BasicAction)actions.get(i)).execute();
}
}
it seems that if the executed action happens to modify "actions" by
deletion then the variable "n" will still reflect the original size and
so allow a bad address. It looks like this iteration construct, rather
than one using the Iterator class, helps get around the concurrent
modification exception that would otherwise occur when I try to change
the schedule. If you mean to allow schedule changes within
BasicAction.execute() shouldn't the size be re-set within the loop, or
some other precaution taken?
Thanks
-- Walt
|