After iterating with an array of iterator, a concurrent access exception is thrown when an array is incremented during an iteration (where the iterator itself does not provide an additional action method):
Exceptions are as follows:
Exception in thread "main" java.util.ConcurrentModificationException
Reason:
A concurrency exception occurs because the iterator does not know that the array has changed when an element is removed from the arrays, and it iterates over the original array element.
Examples are as follows:
Public Static void Main (string[] args) { List<String> list=new LinkedList (); List.add ("AAAAA"); List.add ("bbbbb"); List.add ("CCCCC"); Iterator it=list.iterator (); while (It.hasnext ()) { string name=(String) it.next (); List.add (name);//delete the array here } }
To correct this exception:
Subclass of Iterator: Listiterator
Public Static void Main (string[] args) { List<String> list=new LinkedList (); List.add ("AAAAA"); List.add ("bbbbb"); List.add ("CCCCC"); Listiterator it=list.listiterator (); while (It.hasnext ()) { string name=(String) it.next (); It.add ("Sssssss")//////////By using Listiterator's own method to increase and revise it, there will be no exception. } }
Java--iterator Iteration Problem: Collection Concurrent access exceptions