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,但我想让它成为一个内部类,有什么建议可以让它这样吗?

4

1 回答 1

8

写吧:

Node<E> node = Node.this;

它访问封闭的外部节点实例

于 2012-04-26T15:06:06.353 回答