0

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 ListItems:

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;
        }
    }
Lucca Baumgärtner
  • 197
  • 1
  • 2
  • 8

2 Answers2

0

Your MyList class implements Iterable instead of implements Iterable<ListItem>. The compiler is providing a warning to you about this; pay attention to warnings.

With your update, you should use implements MyListInterface<ListItem>. You are in fact getting the warning I mentioned (use of raw type MyListInterface)--these warnings are intended to help you avoid and resolve problems like this.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • MyList is implementing another interface which extends Iterable. I'm not allowed to change anything in the interface, how do i make it work? – Lucca Baumgärtner Jan 11 '20 at 09:05
  • @LuccaBaumgärtner Can you show that part of the code as well? We need a [mcve]. – Sweeper Jan 11 '20 at 09:07
  • In the exercise it says that ```ListItem``` should by a subclass of MyList with private access, so i can't use it as type when implementing ```MyListInterface```. I added ```ListItem``` to the question. Is there a way to make this work by using a generic type when implementing ```MyListInterface``` – Lucca Baumgärtner Jan 11 '20 at 09:53
  • You have _another_ raw type that is giving you a warning. Perhaps you should have `class ListItem { T value; }` and `MyList> implements MyListInterface`? – chrylis -cautiouslyoptimistic- Jan 11 '20 at 10:37
0

I'm assuming that you have mistakenly wrote that MyList implements MyListInterface {, forgetting to add the generic argument. This means that you are using a raw type, and you should not do this. See What is a raw type and why shouldn't we use it?

The raw type MyListInterface has the super-interface of the raw type Iterator. Once you used one raw type, all the related types become raw (learn more about this in the JLS section 4.8), so currently your MyList implements the non-generic Iterable, which returns a non generic Iterator, whose next method returns Objects. To make MyList implement Iterable<ListItem>, you need to write

class MyList implement MyListIterator<ListItem> {

If your ListItem is not Comparable<ListItem> yet, implement that as well.

Sweeper
  • 213,210
  • 22
  • 193
  • 313