在链接列表上实现迭代器

时间:2018-03-31 14:28:10

标签: java iterator singly-linked-list iterable

我正在使用标准实现中的一些方法创建LinkedList的实现。将Iterator功能添加到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++;
  }

}

1 个答案:

答案 0 :(得分:1)

这是因为您提供了自己的Iterable实施。要使for-each生效,您必须实施java.util.Iterable