I wanted to display the numbers occuring in a character list and then remove that number. Here is my code:
package mytrials;
import java.util.ArrayList;
import java.util.ListIterator;
public class MyTrials {
public static void main(String[] args) {
ArrayList<Character> list = new ArrayList<Character>();
list.add('a');
list.add('1');
list.add('5');
System.out.println(list.size());
for( ListIterator i = list.listIterator(list.size()); i.hasPrevious();){
Character c = (Character) i.previous();
if( Character.isDigit(c)){
System.out.println(c + " is a digit");
list.remove(c);
}
}
System.out.println(list.size());
}
}
Here is the error message:
3
5 is a digit
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$ListItr.previous(AbstractList.java:386)
at mytrials.MyTrials.main(MyTrials.java:27)
Java Result: 1
What is the cause of this error and how can it be rectified.