简体   繁体   English

java嵌套类的数据成员

[英]java nested class data member

I have an AVLTREE class, and inside it an inner class which is an iterator. 我有一个AVLTREE类,在其中有一个内部类,它是一个迭代器。 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? sysrem将显示“ null”,尽管树还具有另一个值:19.在这种情况下,如何处理特定Iterator的实例并将其当前节点更改为已删除节点的后继节点?

Thanks! 谢谢!

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. 在遍历基础集合的同时修改基础集合是一个有问题的情况,这就是为什么当您在for循环中从中删除内置Java集合时会抛出ConcurrentModificationException的原因。 iterator.remove() is the safer way to remove elements from the collection. iterator.remove()是从集合中删除元素的更安全方法。

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. 至于使用迭代器,除非您将集合的每个迭代器存储在某个地方并且让迭代器允许内部“调整”其状态,否则这是不可能的,从长远来看这是不可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM