I’m writing a node class, and i want to make an inner node iterator class, I’ve written so far:
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();
}
}
}
I want to make a node object inside the NodeIterator like this: Node<E> node = this;.
The commented code was written in Node class, I was implementing the Iterator in the Node class itself, but I want to make it an inner class, any suggestion how to make it like that?
Just write:
It accesses the enclosing outer Node instance