java.util.Iterator但无法导入java.util.Iterator [英] java.util.Iterator but cannot import java.util.Iterator
问题描述
鉴于此代码
import java.util.Iterator;
private static List<String> someList = new ArrayList<String>();
public static void main(String[] args) {
someList.add("monkey");
someList.add("donkey");
//Code works when I change Iterator to java.util.Iterator, but import
//is not possible?
for(Iterator<String> i = someList.iterator(); i.hasNext(); ) {
String item = i.next();
System.out.println(item);
}
}
我收到错误:Iterator类型不是通用的;它不能用参数参数化
I receive the error: The type Iterator is not generic; it cannot be parameterized with arguments
Eclipse告诉我导入java.util.Iterator与同一文件中定义的类型冲突。
Eclipse tells me that the import java.util.Iterator conflicts with a type defined in the same file.
推荐答案
我收到错误:Iterator类型不是通用的;它不能用参数参数化
I receive the error: The type Iterator is not generic; it cannot be parameterized with arguments
Eclipse告诉我导入java.util.Iterator与同一文件中定义的类型冲突。
Eclipse tells me that the import java.util.Iterator conflicts with a type defined in the same file.
我能解决这两个确切错误的唯一方法是调用我的类 Iterator
。我想如果你正在写一个关于迭代的小测试类,这将是一个容易犯的错误:
The only way I can get those two exact errors is to call my class Iterator
. I suppose this would be an easy mistake to make if you were writing a little test class about iteration:
import java.util.Iterator;
import java.util.ArrayList;
import java.util.List;
public class Iterator {
private static List<String> someList = new ArrayList<String>();
public static void main(String[] args) {
someList.add("monkey");
someList.add("donkey");
for (Iterator<String> i = someList.iterator(); i.hasNext();) {
String item = i.next();
System.out.println(item);
}
}
}
解决方案:不要做那。把它叫做别的。
Solution: don't do that. Call it something else.
尽管你试着猜测你的代码看起来很有趣,但是你在问题中发布了一个完整的例子是一个短暂的过程。我不排除有另一个代码示例产生这些错误,虽然我没有找到一个经过一些实验。
这篇关于java.util.Iterator但无法导入java.util.Iterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!