增强的 For-Loop 会引发自定义集合实现 Iterable 接口的编译错误

Mat*_*sta 2 java collections iterator iterable

我正在尝试用 Java 创建一个递归列表数据结构,类似于函数式语言中的列表。

我希望它能够实现Iterable,以便可以在for-each 循环中使用。

所以我实现了iterator()创建 的方法Iterator,并且这个循环工作正常(list属于类型RecursiveList<Integer>):

for (Iterator<Integer> it = list.iterator(); it.hasNext(); ) {
    Integer i = it.next();
    System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)

现在我的印象是for (int i : list)基本上只是上面 -loop 的语法糖for,但是当我尝试使用 -each 时for,我收到编译错误:

incompatible types: Object cannot be converted to int
Run Code Online (Sandbox Code Playgroud)

我一生都无法弄清楚为什么它不起作用。这是相关代码:

import java.util.*;

class RecursiveList<T> implements Iterable {

  private T head;
  private RecursiveList<T> tail;
  // head and tail are null if and only if the list is empty
  // [] = { head = null; tail = null}
  // [1,2] = { head = 1; tail = { head = 2; tail = { head = null; tail = null } } }

  public RecursiveList() {
    this.head = null;
    this.tail = null;
  }

  private RecursiveList(T head, RecursiveList<T> tail) {
    this.head = head;
    this.tail = tail;
  }

  public boolean add(T newHead) {
    RecursiveList<T> tail = new RecursiveList<T>(this.head, this.tail);
    this.head = newHead;
    this.tail = tail;
    return true;
  }

  public Iterator<T> iterator() {
    RecursiveList<T> init = this;

    return new Iterator<T>() {
      private RecursiveList<T> list = init;

      public boolean hasNext() {
          return list.head != null;
      }

      public T next() {
          T ret = list.head;
          if (ret == null) throw new NoSuchElementException();
          list = list.tail;
          return ret;
      }
    }
  }
}

class Main {
  public static void main(String[] args) {
    RecursiveList<Integer> list = new RecursiveList<Integer>();

    list.add(1);
    list.add(2);
    list.add(3);

    // works:
    for(Iterator<Integer> it = list.iterator(); it.hasNext();) {
      Integer i = it.next();
      System.out.println(i);
    }
    // output:
    // 3
    // 2
    // 1

    // doesn't work:
    // for (int i : list) System.out.println(i);
  }
}
Run Code Online (Sandbox Code Playgroud)

让我感到真正愚蠢的是我的 IDE 也发现了问题并强调list给出了相同的错误消息,所以我编写我缺少的类型的方式肯定有明显的错误,我只是不知道是什么发生的事情iterator()似乎是Iterator基于更详细的循环工作成功创建了具有正确类型的实例。

Ale*_*nko 6

接口Iterable是通用的,但是您的自定义 Collection 实现了行类型的 iterable,这实际上是Iterable<Object>. 因此,从增强型for循环内的集合中检索的元素将被视为 类型Object

您需要将集合的声明更改为:

class RecursiveList<T> implements Iterable<T>
Run Code Online (Sandbox Code Playgroud)