【发布时间】:2014-09-22 14:06:00
【问题描述】:
我的界面有问题。我有一个这样的迭代器接口:
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;
}
之后我创建测试类并在一个空列表中测试迭代:
package com.wrox.algorithms.lists;
import com.wrox.algorithms.iteration.Iterator;
.....
public void testForwardIteration() {
Lists list = createList();
Iterator iterator = list.iterator(); // <- ERROR
}
我收到此错误:
类型不匹配:无法从 java.util.Iterator 转换为 com.wrox.algorithms.iteration.Iterator
你知道我的错误在哪里吗?感谢您的支持!
【问题讨论】:
-
createList()可能返回实现java.util.Iterator的List。我们需要看到这一点才能确定。 -
谢谢朋友的回答!
-
我还有两个接口:Lists 和 Iterable。 Iterable 只包含一个 iterator() 方法。我忘了在列表中导入这个接口。如果你有课外时间,你能给我解释一下吗?为什么机器给我这个不匹配。我发现了错误,但我不明白这一点。感谢您的宝贵时间!
标签: java interface iterator type-mismatch