1

I need some help in Understanding the underlying behavior while adding elements into a LinkedList using ListIterator in java. Please look at the code below:

Here, graphQueue initially has only one Node. And that Node has three children which are also Nodes. My logic is to remove the main Node and add its children into the graphQueue for iterating over them and add their children into the queue and so on... Let's say I have vertex 0 which is added in queue; and it has three children 2, 3, & 5. I am removing 0 from Queue and I am adding 2, 3 & 5 into the queue.

ListIterator<Node> it = graphQueue.listIterator();
while (it.hasNext())
{
   Node node = it.next();
   it.remove();

   if (node.hasChildren())
   {
      for (Node child : node.getChildren())
      {
         it.add(child);
      }
   }
}

Now the problem is the loop exits after first loop, but it works if I put one more do while loop around this while loop and creating Iterator object again. Please look at the below code:

ListIterator<Node> it = graphQueue.listIterator();
do
{
  while (it.hasNext())
  {
     Node node = it.next();
     it.remove();

     if (node.hasChildren())
     {
        for (Node child : node.getChildren())
        {
           it.add(child);
        }
     }
  }

  it = graphQueue.listIterator();
} while(it.hasNext());

Am I missing something? Thanks!

1 Answer 1

3

According to the documentation of ListIterator.add() the element you add is placed before the iterator's next element. That means even though you modify your list by adding an element, your current iterator won't consider it for its traversal. That's why your iteration stops after first loop.

In your second scenario. You have two nested loops, At the end of inner loop you create a new iterator for the list. This iterator is a fresh one which is starting from the beginning of the list again. So this piece of code works as you expected.

Sign up to request clarification or add additional context in comments.

6 Comments

Documentation says - "The element is inserted immediately before the element that would be returned by next(), if any, and after the element that would be returned by previous(), if any." In my situation there are no elements in the list, as I am performing remove operation before add(), so can you explain me more clearly what would be happening?
Moreover, I am adding three elements, as per the definition at least the pointer should be pointing to second element that I added. Ahh! Or is it that all the three nodes are added just before the next pointer? If it is so, what I should be doing to make sure the pointer points to the first element?
Yes all three nodes just added before next pointer. No matter what amount of nodes you add, your iterator's next node will be the same. I just gave you an explanation on why your two piece of code behave differently. I think that is what you asked. But its not clear for me your ultimate goal of this code. Do your goal is to traverse through all the nodes of your tree? If it is the case, then there are other approaches you can take such as recursive tree traversal methods.
Yeah! Thanks for that, now I have got clarity on what is happening there. And I am not seeking complete solution for what I am working on, I am exploring Collections and I am trying to use LinkedList and ListIterator. Now I understand this Java concept, I will have to re-write the logic. Thanks! One last question - is there any way to make the iterator object to point to the location that I want? like making the pointer to go to the first location.
You can add it.previous(); call, just after it.add(child); to move the iterator to your desired position
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.