双重链表上的Java迭代器

时间:2022-09-05 19:51:20

Hi I'm very new to Java and have this problem with building a nested Iterator class for a Doubly Linked List. I wasn't sure how to write a public E next() method to have it iterate through a Doubly-Linked-List.

嗨,我对Java很新,并且在为双向链接列表构建嵌套的Iterator类时遇到了这个问题。我不知道如何编写公共E next()方法让它迭代通过Doubly-Linked-List。

Any help is greatly appreciated!

任何帮助是极大的赞赏!

  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

3 个解决方案

#1


Try this:

public boolean hasNext() {
  return current != null;
}
public E next() {
    if (!hasNext()) throw new NoSuchElementException();
    E tmp = current.item;
    current = current.next;  // if next is null, hasNext will return false.
    return tmp;
}

Also drop last and index, you dont need them.

也放弃最后和索引,你不需要它们。

#2


 public E next() {
        if (!hasNext()) throw new NoSuchElementException();
             current = current.next;
    return current;
    }

#3


you may want to take a look at java.util.LinkedList :

你可能想看一下java.util.LinkedList:

From Documentation :

来自文档:

Doubly-linked list implementation of the List and Deque interfaces. Implements all optional list operations, and permits all elements (including null). All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

List和Deque接口的双链表实现。实现所有可选列表操作,并允许所有元素(包括null)。对于双向链表,所有操作都可以预期。索引到列表中的操作将从开头或结尾遍历列表,以较接近指定索引为准。

LinkedList<String> linkedlist = new LinkedList<String>();

     //add(String Element) is used for adding

     linkedlist.add("Item1");
     linkedlist.add("Item5");
     linkedlist.add("Item3");

      /*Add First and Last Element*/

     linkedlist.addFirst("First Item");
     linkedlist.addLast("Last Item");

     //you can get the iterator by 
     ListIterator<String> it = linkedlist.listIterator();

#1


Try this:

public boolean hasNext() {
  return current != null;
}
public E next() {
    if (!hasNext()) throw new NoSuchElementException();
    E tmp = current.item;
    current = current.next;  // if next is null, hasNext will return false.
    return tmp;
}

Also drop last and index, you dont need them.

也放弃最后和索引,你不需要它们。

#2


 public E next() {
        if (!hasNext()) throw new NoSuchElementException();
             current = current.next;
    return current;
    }

#3


you may want to take a look at java.util.LinkedList :

你可能想看一下java.util.LinkedList:

From Documentation :

来自文档:

Doubly-linked list implementation of the List and Deque interfaces. Implements all optional list operations, and permits all elements (including null). All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

List和Deque接口的双链表实现。实现所有可选列表操作,并允许所有元素(包括null)。对于双向链表,所有操作都可以预期。索引到列表中的操作将从开头或结尾遍历列表,以较接近指定索引为准。

LinkedList<String> linkedlist = new LinkedList<String>();

     //add(String Element) is used for adding

     linkedlist.add("Item1");
     linkedlist.add("Item5");
     linkedlist.add("Item3");

      /*Add First and Last Element*/

     linkedlist.addFirst("First Item");
     linkedlist.addLast("Last Item");

     //you can get the iterator by 
     ListIterator<String> it = linkedlist.listIterator();