简体   繁体   中英

Type mismatch with Iterator interface

I have a problem with an interface. I have an Iterator interface like this:

package com.wrox.algorithms.iteration;
import com.wrox.algorithms.lists.IteratorOutOfBoundsException;

public interface Iterator {
    public void first();
    public void last();
    public void isDone();
    public void next();
    public Object current() throws IteratorOutOfBoundsException;
}

After that I create test class and test the iteration in an empty list:

package com.wrox.algorithms.lists;
import com.wrox.algorithms.iteration.Iterator;

.....

public void testForwardIteration() {
    Lists list = createList();

    Iterator iterator = list.iterator(); // <- ERROR
}

I get this error:

Type mismatch: cannot convert from java.util.Iterator to com.wrox.algorithms.iteration.Iterator

Have you any idea where my mistake is? Thank you for your support!

list.iterator() returns a java.util.Iterator, not a com.wrox.algorithms.iteration.Iterator

Edit: Just noticed you have a "Lists" and not a "List". You also need a List from java.util.List. Don't create your own List/Lists

If two people have the same name, that doesn't mean you can pretend they are the same person. They're still two separate people. You can be friends with both, but if you mix them up they will be annoyed with you.

Similarly, if two interfaces are named Iterator , that doesn't mean you can pretend they are the same interface. java.util.Iterator and com.wrox.algorithms.iteration.Iterator are two separate interfaces. You can use both, but if you mix them up the compiler will yell at you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM