5

公共类 C1 实现 Iterable { 私有 LinkedList 列表;public static class NC1 { ... } ... x public Iterator iterator() { return list.iterator(); } }

但是日食抱怨(在x-ed线上):

- The return type is incompatible with Iterable<NC1>.iterator()
- implements java.lang.Iterable<NC1>.iterator

我不明白错误在哪里。有人可以帮忙吗?

4

2 回答 2

9

您需要更改NC1C1.NC1. 以下编译:

import java.util.*;

public class C1 implements Iterable<C1.NC1> {
    private LinkedList<NC1> list;
    public static class NC1 {
    }

    public Iterator<C1.NC1> iterator() {
        return list.iterator();
    }
}

或者,您可以import static yourpackage.C1.NC1.

于 2010-05-31T13:32:26.457 回答
2

这段代码编译得很好:

public class C1 implements Iterable<NC1> {
    public static class NC1 {
    }

    private LinkedList<NC1> list;

    public Iterator<NC1> iterator() {
        return this.list.iterator();
    }
}

, 所以你省略的部分一定有错误

编辑:

看到另一个答案后:

是的,我打开了自动导入,所以你需要这一行:

import com.yourpackage.C1.NC1;
于 2010-05-31T13:34:11.653 回答