I wrote a custom iterator for the class MyList
this is what it looks like:
private class MyListIterator implements Iterator<ListItem> {
// Constructor
public MyListIterator() {
nextElement = head;
}
@Override
public ListItem next() {
...
}
Then in the class MyList
i create a new MyListIterator
like this:
@Override
public Iterator<ListItem> iterator() {
MyListIterator iterator = new MyListIterator();
return iterator;
}
and finally i try to iterate over a MyList
Object expecting ListItem
s:
MyList list = new MyList();
for (ListItem item : list ) {
System.out.println(item.value);
}
But i get the incompatible types error because the iterator returns Object
types instead of ListItem
. How do i fix it?
Edit: MyList
implements MyListInterface
:
public interface MyListInterface<T extends Comparable<T>> extends Iterable<T> {
public Iterator<T> iterator();
}
But I'm not allowed to change anything in MyListInterface
private class ListItem {
Comparable value;
ListItem previous;
ListItem next;
public ListItem(Comparable value) {
this.value = value;
}
}