2
我正在写一个节点类这种情况下,我想打一个内部节点迭代器类,我至今写:调用一个内部类
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Node<E> {
E data;
Node<E> next;
int current = 0;
public Node(E data, Node<E> next){
this.data = data;
this.next = next;
}
public void setNext(Node<E> next){
this.next = next;
}
private class NodeIterator implements Iterator {
/*@Override
public boolean hasNext() {
Node<E> node = this;
for(int i=1; i<current; i++){
node = node.next;
}
if(node.next==null){
current = 0;
return false;
}
current++;
return true;
}*/
@Override
public boolean hasNext() {
// code here
}
/*public Node<E> next() {
if(next==null){
throw new NoSuchElementException();
}
Node<E> node = this;
for(int i=0; i<current && node.next!=null; i++){
node = node.next;
}
return node;
}*/
@Override
public Node<E> next() {
// code here
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
}
我想打一个节点对象在NodeIterator内部像这样:Node<E> node = this;
。
评论代码是用Node类编写的,我在Node类本身实现了Iterator,但是我想让它成为一个内部类,任何建议如何使它成为那样?
所以,你要访问的外部节点封闭外节点实例?试试'节点 node = Node.this;'。另请注意,您可能想要缓存迭代器中的下一个节点,而不是从第一个节点迭代到当前节点。 –
Thomas
2012-04-26 15:06:47
谢谢托马斯:D – 2012-04-26 15:08:07