Nested iterators (well, not nested exactly...)

Discussion in 'Java' started by Russ Perry Jr, Aug 19, 2004.

  1. Is it safe to "nest" iterators? In other words, if I'm iterating over
    a Vector, and in the middle of doing that (specifically after removing
    an element) I iterate over the same Vector to find something out in
    the middle if the iterator loop, is that okay?

    It goes something like this:


    private Vector listOfSessions; // vector of SessionObjects


    public boolean isLoginNameStillOnList(String loginNameToCheck)
    {
    boolean result = false;

    for (Iterator iter = listOfSessions.iterator();
    iter.hasNext();)
    {
    if (((SessionObject) iter.next()).getLoginName().
    equalsIgnoreCase(loginNameToCheck))
    {
    result = true;
    break;
    }
    }

    return result;
    }


    public boolean removeSessionFromList(String sessionID)
    {
    boolean result = false;

    for (Iterator iter = listOfSessions.iterator();
    iter.hasNext();)
    {
    SessionObject sessionObject = (SessionObject) iter.next();

    if (sessionObject.getSessionID().equalsIgnoreCase(sessionID))
    {
    /* ignore return value */ iter.remove();

    // check if there are no other instances of loginName
    // in the list...
    if (!isLoginNameStillOnList(sessionObject.getLoginName()))
    {
    // ...if not, log them out of database entirely
    logoutFromDatabase(sessionObject);
    }

    result = true;
    break;
    }
    }

    return result;
    }
     
    Russ Perry Jr, Aug 19, 2004
    #1
    1. Advertising

  2. Russ Perry Jr wrote:

    > Is it safe to "nest" iterators? In other words, if I'm iterating over
    > a Vector, and in the middle of doing that (specifically after removing
    > an element) I iterate over the same Vector to find something out in
    > the middle if the iterator loop, is that okay?


    Yes, as long as you don't modify the Vector during any iteration except
    via the corresponding Iterator. If you have two Iterators active over
    the same Collection at the same time then modifying the collection via
    either one will require that you stop using the other. That doesn't
    appear to be a problem in your code.


    John Bollinger
     
    John C. Bollinger, Aug 20, 2004
    #2
    1. Advertising

  3. "John C. Bollinger" <> wrote:
    > Russ Perry Jr wrote:
    > > Is it safe to "nest" iterators? In other words, if I'm iterating over
    > > a Vector, and in the middle of doing that (specifically after removing
    > > an element) I iterate over the same Vector to find something out in
    > > the middle if the iterator loop, is that okay?


    > Yes, as long as you don't modify the Vector during any iteration except
    > via the corresponding Iterator. If you have two Iterators active over
    > the same Collection at the same time then modifying the collection via
    > either one will require that you stop using the other. That doesn't
    > appear to be a problem in your code.


    Okay, cool.

    I think we'll probably end up changing it anyway because a post in
    another thread -- I think by you as well -- got me thinking about
    threads and synchronization...

    But thanks very much for confirming this; it wasn't evident from
    the javadoc if the Vector iterator() returns unique iterators or
    the same one (I suppose it would be a class method rather than an
    object method if so, but better safe than sorry).
    --
    //*================================================================++
    || Russ Perry Jr 2175 S Tonne Dr #114 Arlington Hts IL 60005 ||
    || 847-952-9729 [NEW!] VIDEOGAME COLLECTOR! ||
    ++================================================================*//
     
    Russ Perry Jr, Aug 20, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    510
    Kai-Uwe Bux
    May 8, 2005
  2. Patrick Kowalzick

    Iterators over nested STL container

    Patrick Kowalzick, Aug 3, 2005, in forum: C++
    Replies:
    2
    Views:
    406
    Chris Theis
    Aug 3, 2005
  3. magical_muggle

    Question about nested vectors/iterators

    magical_muggle, May 7, 2007, in forum: C Programming
    Replies:
    0
    Views:
    588
    magical_muggle
    May 7, 2007
  4. , India
    Replies:
    10
    Views:
    1,099
    James Kanze
    Aug 8, 2009
  5. Josselin
    Replies:
    0
    Views:
    111
    Josselin
    Sep 24, 2007
Loading...

Share This Page