Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Iterator Problem

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Iterator Problem

    Dear All,

    I have a problem on using the iterator.

    Why should I run “iter = myList.iterator();” one more time in order to get the correct results in TestLinkedList.java?

    Please find the attached file for your reference.

    Thank you very much!

    Best Regards,
    Chris.
    Attached Files Attached Files


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Iterator Problem

    The iterator should not be re-used after a collection has been modified, in fact according to the java API if you try to use the iterator after the collection has been modified a ConcurrentModificationException may be thrown. As per the java API:
    Note that Iterator.remove is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.
    The Collection Interface (The Java™ Tutorials > Collections > Interfaces)

  3. #3
    Junior Member kite98765's Avatar
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Iterator Problem

    Quote Originally Posted by chrisych View Post
    Dear All,

    I have a problem on using the iterator.

    Why should I run “iter = myList.iterator();” one more time in order to get the correct results in TestLinkedList.java?

    Please find the attached file for your reference.

    Thank you very much!

    Best Regards,
    Chris.
    It'd be much easier if you placed your code useing the provided shortcut. If you read the "before you post" sticky thread, you would know to put your code in this:

    If this is your first time here, there a few rules we would like you to follow before posting:

    1. Use code blocks for your code, please! To use code blocks, put [ code] before the code and [/ code] after it. It will look like this:


    Java Code
    Some code

Similar Threads

  1. Iterator, with ArrayList
    By rsala004 in forum Collections and Generics
    Replies: 3
    Last Post: October 25th, 2009, 09:00 AM