I’m trying to iterate through a Integer ArrayList and getting the value at each element, but I’m getting an error at the int value = ….
Not sure whats happening. Please advise.
Iterator<Integer> listItr = executeList.iterator(); // iterator for the execute list
while (listItr.hasNext()) { // iterate through list and do work!
int robIndex = listItr.next();
int timer = fakeRob.exeCountDown(robIndex); // decrement first then return timer
if (timer == 0) {// check if instr finished execution
System.out.println("timer expired. fire");
executeList.remove(executeList.indexOf(robIndex)); // 1. remove instr from exeList
transitState(robIndex, EX, WB); // 2. transit from EX state to WB state
int tag = fakeRob.getTag(robIndex); // get producer tag
regFile.setRdy(tag); // 3a. set register file ready flag for this tag
fakeRob.wakeUp(tag); // 3b. wake up instructions with this tag
}
}
Error:
java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at sim.execute(sim.java:180)
at sim.<init>(sim.java:71)
at sim.main(sim.java:270
Thanks,
Hank
Perhaps if you put what it is you’re doing inside the loop it would help. If you’re trying to remove an element from the list, you need to call listItr.remove() to do it. In general, you should not be calling any functions in the loop which modify the list (ie. add(), set(), etc…).
The following code would trigger this
The proper way to do it would be:
Also other threads (as mentioned above) could be the issue. Remember, iterators are backed by the list itself in all the java supplied collections. So if another thread modifies the list while you’e iterating, you’ll run into this.