Java Collection 4 (iterator)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.