Hi I want to implement the iterator for a class that extends abstract Collection.
class Name<E extends Comparable<E>>
extends AbstractCollection<CompRational>
implements Iterable<E>{
...
public Iterator<E> iterator(){
return new NameIterator<E>(this);
}
...
}
and the Iterator should look like this
class NameIterator<E extends Comparable<E>> implements Iterator<E>{
private Name<E> tree;
private int pos = 0;
public NameIterator ( Name<E> t) {
...
}
@Override
public boolean hasNext() {
...
}
@Override
public E next () {
...
}
@Override
public void remove(){
throw new UnsupportedOperationException();
}
}
The error message that I receive is that my class Name does not override or implement Iterator and the iterator cannot override iterator because the types are incompatible and it says that I'm using two different but where and how and how can i get rid of it?
Don't have the
Collection
implementIterator
. That's for theIterator
instance. Your concrete collection needs to extendAbstractCollection
, and yourNameIterator
implementsIterator
.