Question

public class C1 implements Iterable { private LinkedList list; public static class NC1 { ... } ... x public Iterator iterator() { return list.iterator(); } }

but eclipse whines (at the x-ed line):

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

I don't understand where the mistake is. Can someone help?

Was it helpful?

Solution

You need to change NC1 to C1.NC1. The following compiles:

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();
    }
}

Alternatively, you could import static yourpackage.C1.NC1.

OTHER TIPS

this code compiles just fine:

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

    private LinkedList<NC1> list;

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

, so there must be an error in a part you omitted

EDIT:

after seeing the other answer:

yes, I have auto-imports switched on, so you need this line:

import com.yourpackage.C1.NC1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top