0

I have an AVLTREE class, and inside it an inner class which is an iterator. The iterator is instanciated by the user request only. Suppose I have this code:

tree.add(10);
tree.add(6);
tree.add(19);

Iterator<Integer> it1 = tree.iterator();
System.out.println(it1.next());
System.outrintln(it1.next());

tree.delete(10);

System.outrintln(it1.next());

the sysrem will print "null" , althogh the tree has another value: 19. How do I approach the instance of a specific Iterator and change its current node to be the successor of the deleted node in such cases?

Thanks!

3
  • try it1.remove() if it1.next() == 10 Commented May 17, 2014 at 8:47
  • problem is: I did not implement remove() method (was not in excersize description), and it has to be in the class section not in the main method. Commented May 17, 2014 at 8:49
  • it all depends on the implementation of you iterator and datastructure, and what your requirements are. Commented May 17, 2014 at 8:51

1 Answer 1

1

Modifying the underlying collection while iterating through them is a problematic case, that's why the inbuilt collections of Java would be throwing ConcurrentModificationException at your face when you deleted from them while in a for loop, for example. iterator.remove() is the safer way to remove elements from the collection.

As for approaching the iterator, you can't unless you store every iterator of the collection somewhere and have the iterators allow internal "tweaking" of their status, which isn't very feasible in the long run.

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

Comments

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.