This discussion is archived
4 Replies Latest reply: Jun 22, 2008 11:11 AM by 807589 RSS

Add elements in a collection while iterating

807589 Newbie
Currently Being Moderated
Hello.
I want to insert new elements in a collection while I am iterating over it. For that, I am using ListIterator interface:
...
it = _chart.elementAt(i).listIterator();
     while (it.hasNext()) {
             State scan = (State) it.next();
          
          boolean incomplete_flag = scan.isIncomplete();
          boolean next_cat_flag = scan.NextCatIsNotPOS(_grammar, _sentence);

          if (incomplete_flag && next_cat_flag) {
               predictor(scan);
...
The predictor method will call the following method:
public void enqueue(State state, int index)
       if (!_chart.elementAt(index).contains(state)) {
          it.add(state);
}
The chart object is declared like this:Vector<Stack<State>> _chart.
The it variable is a class variable.

My problem is that I am able to insert the state correctly in the chart but the while loop, where I iterate over a specific position in a chart
does not continue, like the new state wasn't inserted.

Edited by: broshlir on Jun 22, 2008 10:45 AM

Edited by: broshlir on Jun 22, 2008 10:46 AM
  • 1. Re: Add elements in a collection while iterating
    807589 Newbie
    Currently Being Moderated
    I wouldn't use an iterator in this scenario. I would use a for loop because you are probably going to throw a ConcurrentModificationException or just have bad results if your trying to add things while iterating.

    Also, consider a different approach to the problem like using a Queue and polling from that.
  • 2. Re: Add elements in a collection while iterating
    807589 Newbie
    Currently Being Moderated
    broshlir wrote:
    My problem is that I am able to insert the state correctly in the chart but the while loop, where I iterate over a specific position in a chart
    does not continue, like the new state wasn't inserted.
    Do you want the while loop to consider the newly inserted element? In that case I think you should call it.previous() immmediately after it.add(state) in enqueue.
  • 3. Re: Add elements in a collection while iterating
    807589 Newbie
    Currently Being Moderated
    Nether wrote:
    I wouldn't use an iterator in this scenario. I would use a for loop because you are probably going to throw a ConcurrentModificationException or just have bad results if your trying to add things while iterating.
    There may be many reasons for not modifying a data structure during iteration but that it wouldn't work isn't one of them. In the documentation for ListIterator:

    "An iterator for lists that allows the programmer to traverse the list in either direction, *modify the list during iteration*, and obtain the iterator's current position in the list"
  • 4. Re: Add elements in a collection while iterating
    807589 Newbie
    Currently Being Moderated
    Yeah, i was thinking about Iterators not ListIterators, but still using a ListIterator in this way seems ugly. In the docs it says that add(Object) is an optional method anyways.