I was writing a sample plan to demonstate how to withdraw elments from list spell iterating over it past times using Iterator.remove() mehtod. Unfortunately I was getting next error, correct at the house where I was calling the Iterator.remove() method :
Exception inward thread "main" java.lang.IllegalStateException
at java.util.ArrayList$Itr.remove(Unknown Source)
at dto.IteratorRemoveTest.main(IteratorRemoveTest.java:25)
I was puzzled, why I am getting this error because I was using Iterator's remove() method in addition to non the ArrayList's remove() method, which causes ConcurrentModificationException inward Java.
By carefully looking at code in addition to error message I afterwards figure out that I was calling Iterator.remove() method without calling the Iterator.next(), which was causing this probelm.
To educate this only telephone phone the Iterator.next() earlier calling Iterator.remove() method, every bit shown below:
Exception inward thread "main" java.lang.IllegalStateException
at java.util.ArrayList$Itr.remove(Unknown Source)
at dto.IteratorRemoveTest.main(IteratorRemoveTest.java:25)
I was puzzled, why I am getting this error because I was using Iterator's remove() method in addition to non the ArrayList's remove() method, which causes ConcurrentModificationException inward Java.
java.lang.IllegalStateException during Iterator.remove()
Here is my code, Can you lot topographic point what is wrong, without looking at the solutionimport java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; /** * Java Program to demonstrate how to withdraw elements from List spell * traversing over it using Iterator. * * @author WINDOWS 8 */ public class IteratorRemoveTest { public static void main(String args[]) { List<String> brands = new ArrayList( Arrays.asList("Apple", "Microsoft, Google")); System.out.println("Java Program to withdraw elements from listing via Iteator"); System.out.println("List earlier removing elements: " + brands); for (final Iterator<String> itr = brands.iterator(); itr.hasNext();) { itr.remove(); } System.out.println("List after removing elements: " + brands); } } Output: Java Program to withdraw elements from listing using Iteator List earlier removing elements: [Apple, Microsoft, Google] Exception inward thread "main" java.lang.IllegalStateException at java.util.ArrayList$Itr.remove(ArrayList.java:849) at IteratorRemoveTest.main(IteratorRemoveTest.java:25) Java Result: 1
By carefully looking at code in addition to error message I afterwards figure out that I was calling Iterator.remove() method without calling the Iterator.next(), which was causing this probelm.
To educate this only telephone phone the Iterator.next() earlier calling Iterator.remove() method, every bit shown below:
for (final Iterator<String> itr = brands.iterator(); itr.hasNext();) { itr.next(); itr.remove();}
Now, if you lot run the Java program, you lot volition larn next output
Java Program to remove elements from list using Iteator List before removing elements: [Apple, Microsoft, Google] List after removing elements: []
Our code ran successfully. Here is the dainty summary of things you lot should know spell trying to delete elments from Java collection or listing using Iteartor's remove() method:
That's all almost how to solve Exception inward thread "main" java.lang.IllegalStateException during Iterator.remove() inward Java program. Sometime, you lot construct featherbrained mistakes which causes error looking similar a big monster. So, if you lot can't run across the error, accept a interruption in addition to they hold off it again, you lot volition uncovering it.
Related Java Error in addition to Exception guides:
- SQLServerException: The index two is out of gain (solution)
- Error: could non opened upwards 'C:\Java\jre8\lib\amd64\jvm.cfg' (solution)
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver [solution]
- 'javac' is non recognized every bit an internal or external ascendence (cause)
- java.lang.ClassNotFoundException: org.Springframework.Web.Context.ContextLoaderListener [fix]
- java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver [fix]
- How to Fix java.lang.OufOfMemoryError: Direct Buffer Memory (solution)
- java.lang.OutOfMemoryError: Java heap infinite : Cause in addition to Solution (fix)
0 Response to "Exception inwards thread top dog java.lang.IllegalStateException during Iterator.remove()"
Posting Komentar