繁体   English   中英

在链接列表上实现迭代器

[英]Implementing Iterator on a Linked List

我仅使用标准实现中的几种方法来创建LinkedList的实现。 将迭代器功能添加到LinkedList时遇到了问题。

现在我可以添加一些元素,然后添加到这样的内容中

class testList {

   public static void main(String[] args){

      MyLinkedList<String> list = new MyLinkedList<String>();
      list.add("Element 0");
      list.add("Element 1");
      list.add("Element 2");

      Iterator<String> iter = list.iterator();

      System.out.println(iter.next());
      System.out.println(iter.next());
      System.out.println(iter.next());

      for (String s : list){
        System.out.println(s);
      }
    }
  }

除了最后一部分,其他所有东西都可以正常工作,当尝试对其进行编译时,我得到一个错误提示错误:for-each不适用于(String s:list)的表达式类型

在实现过程中我是否忽略了一些显而易见的事情?

这是我的代码的其余部分,我将自定义接口用于Iterable和List

interface List<T> extends Iterable<T> {
public int size();
public void add(int pos, T x);
public void add(T x); 
}

interface Iterator<T> {
boolean hasNext();
T next();
}

interface Iterable<T> {
Iterator<T> iterator();
}

这是代码的主要部分

import java.util.* ;

class MyLinkedList<T> implements List<T> {

  private Node<T> head;
  private Node<T> tail;
  private int currentSize;

  //constructor for class
  public MyLinkedList(){
    this.head = null;
    this.tail = null;
    this.currentSize = 0;
  }

  //Node class used to hold the information, and link to each other
  public class Node<E> {
      private E data;
      private Node<E> next;

      //constructor for node class
      public Node(E data, Node<E> next){
        this.data = data;
        this.next = next;
      }
      public E getData(){
        return this.data;
      }
      public void setData(E newData){
        this.data = newData;
      }
      public Node<E> getNext(){
        return this.next;
      }
      public void setNext(Node<E> newNext){
        this.next = newNext;
      }
  }

  class LinkedListIterator implements Iterator<T>{
    private Node<T> current;
    public LinkedListIterator(){
      current = head;
    }
    public T next(){
      if (current == null){
        throw new NoSuchElementException();
      }
      T temp = current.getData();
      current = current.getNext();
      return temp;
    }
    public boolean hasNext(){
      return current != null;
    }
  }
  public Iterator<T> iterator(){
    return new LinkedListIterator();
  }
  public int size(){
    return this.currentSize;
  }
  public boolean isEmpty(){
    return this.size() == 0;
  }
  public void add(int pos, T x){
    if(pos < 0 || pos > size()){
      throw new IndexOutOfBoundsException();
    }
    if(pos == size()){
      add(x);
      return;
    }
    if (pos == 0){
      head = new Node(x, head);
    }else{
      Node<T> current = head;
      for(int j = 0; j < pos-1; j++){
        current = current.getNext();
      }
      current.setNext(new Node(x, current.getNext()));
    }
    currentSize++;

  }
  public void add(T x){
    if(isEmpty()){
      head = new Node(x, null);
      tail = head;
    }else{
      tail.setNext(new Node(x, null));
      tail = tail.getNext();
    }
    currentSize++;
  }

}

这是因为您提供了自己的Iterable实现。 为了使for-each正常工作,您必须实现java.util.Iterable

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM