Package Cn.lonecloud.iterator;import Java.util.arraylist;import Java.util.listiterator;public class MyListIterator { public static void Main (string[] args) {arraylist<integer> list=new arraylist<integer> (), for (int i = 0; I < ; 10; i++) {list.add (i);} The interface that the list iterator inherits is Iteratorlistiterator<integer> Iterator=list.listiterator (); Listiterator<integer> iterator2=list.listiterator ();/** * Exception in thread "main" Java.lang.IllegalStateExceptionat Java.util.arraylist$listitr.set (arraylist.java:941) at Cn.lonecloud.Iterator.myListIterator.main (MYLISTITERATOR.JAVA:14) also has an error, and the set and remove methods in this class are required and next () The method is used in combination and his changing element is the element returned in next () */iterator.next (); Iterator.set (n);//iterator.next (); Iterator.remove ();/** * Exception in thread "main" Java.util.ConcurrentModificationExceptionat java.util.arraylist$ Itr.checkforcomodification (arraylist.java:901) at Java.util.arraylist$itr.next (arraylist.java:851) at Cn.lonecloud.Iterator.myListIterator.main (mylistiterator.java:28) changed the elements inside the list,Causes this iterator to clutter up, causing the exception to be thrown */while (Iterator2.hasnext ()) {Integer integer = (integer) iterator2.next (); System.out.println (integer);}}}
Listiterator is an interface inherited from the iterator;
There are set methods inside;
Use of Listiterator