Suugaku
Suugaku

Reputation: 2747

Iterator on generics with 'extends'

public class abc<X extends Z> implements Iterable<X>
{
    protected ArrayList<X> list;

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

I get a 'cannot find symbol' error for the iterator method. I have honestly no clue why.

Upvotes: 3

Views: 1629

Answers (1)

Mark Peters
Mark Peters

Reputation: 81074

You should give the exact error message, but my guess is that you need to import the Iterator class (java.util.Iterator).

Also the way you have declared X requires that you have some other class named Z (X is restricted to Z or subclasses of Z). Is this true? If it is, you should rename it as only type parameters should have single-character names. If it's not true, and you are considering Z another type parameter, you would need to declare Z as a type parameter somewhere.

Upvotes: 4

Related Questions