Iterators use the issues to be aware of:
1. Iterators are important when traversing elements:
It is not allowed to use the collection object to change the number of elements in the collection while iterating over the elements.
If you want to add or remove a method to use an iterator.
2. If you use a Class collection object to change the number of elements in the collection, then a java.util.ConcurrentModificationException exception occurs.
The 3 iterator iterates through the elements of the process: iterators from creation to end of use time.
Public classex12 { Public Static voidMain (string[] args) {List List=NewArrayList (); List.add ("Zhang San"); List.add ("John Doe"); List.add ("Harry"); Listiterator it=List.listiterator (); while(It.hasnext ()) {System.out.print (It.next () )+ ","); List.add ("Aa");//Note the Add () method is to add the element to the position where the current pointer is pointing, then move the pointer down one, or else the dead loop//List.add ("Aa");//The Add () method is to add an element to the very end of the collection, which violates the iterator's traversal of the entire collection, since it has not traversed the pointer until the last, throwing the exception} System.out.println ("\ r \ n elements of collection" +list); System.out.println (); //it.add ("AA");It.next (); }}
Java Collection 4 (iterator)