AP® Computer Science A glossary

ConcurrentModificationException

Course: AP® Computer Science A Shows up in: Unit 4: Data Collections

The run-time exception that can be thrown when an ArrayList changes size while an enhanced for loop is traversing it.

Appears in the course and exam description (CED): 4.9.A.4

Do not call add or remove on a list inside an enhanced for loop over that list. To change the size during a traversal, use an indexed loop, for example traversing backward when removing.

ArrayList<String> cart = new ArrayList<String>();
cart.add("tea");
cart.add("milk");
cart.add("rice");
for (String item : cart)
{
    cart.remove(0);
}

Output:

Exception in thread "main" java.util.ConcurrentModificationException
Learn it properly, with a checkpoint 4.9 ArrayList Traversals

Related terms

Was this page helpful?

No names are ever shown, and every public comment is checked by us first. Please do not include personal details. To get a reply, email [email protected].

↑ Top

Enlarged view of the selected figure