tags:

views:

64

answers:

2

Hi, I wrote this code:

    public class C1 implements Iterable<NC1> {
       private LinkedList<NC1> list;
       public static class NC1 {
          ...
       }
       ...
    x  public Iterator<NC1> 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?

+7  A: 

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.

aioobe
that's right, thank you.
Metz
+2  A: 

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;
seanizer
Which version of java / eclipse?
aioobe
eclipse doesn't whine anymore with aioobe's solution.
Metz
Solving it by importing the class is a good answer too!
Andreas_D