Perché il seguente codice genera ConcurrentModificationExcrption
, quando si cancella l'Elenco secondario dopo l'Elenco principale, ma non se si cancella l'elenco secondario e quindi l'Elenco principale?ConcurrentModificationException durante la cancellazione di un sottoelenco
ArrayList<Integer> masterList = new ArrayList<Integer>();
List<Integer> subList;
// Add some values to the masterList
for (int i = 0; i < 10; i++) {
masterList.add(i * i);
}
// Extract a subList from the masterList
subList = masterList.subList(5, masterList.size() - 1);
// The below throws ConcurrentModificationException
masterList.clear();
subList.clear(); // Exception thrown in this line
// The below doesn't throw any exception
subList.clear();
masterList.clear(); // No exception thrown. Confused??
Questo è molto informativo. Grazie. –