嗨,我對Java很陌生,並且在構建雙向鏈接列表的嵌套迭代器類時遇到此問題。我不確定如何編寫一個public E next()
方法,讓它遍歷雙鏈表。雙重鏈接列表上的Java迭代器
任何幫助,非常感謝!
private class DoubleListIterator implements Iterator<E> {
// instance variable
private Node current=head;
private Node last;
private int index=0;
public boolean hasNext() {
return index < N;
}
public E next() {
if (!hasNext()) throw new NoSuchElementException();
}
public void remove() { throw new UnsupportedOperationException(); }
}// end class ListIterator
順便說一下,在我的解決方案中,字段名'current'不正確。如果您接受我的解決方案,請將其重命名爲「next」或「cursor」。 –