java.util.Iterator 但不能导入 java.util.Iterator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24064061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:57:41  来源:igfitidea点击:

java.util.Iterator but cannot import java.util.Iterator

javaiterator

提问by qwertzuiop13

Given this Code

鉴于此代码

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);
    }

}

I receive the error: The type Iterator is not generic; it cannot be parameterized with arguments

我收到错误:迭代器类型不是通用的;它不能用参数参数化

Eclipse tells me that the import java.util.Iterator conflicts with a type defined in the same file.

Eclipse 告诉我导入 java.util.Iterator 与同一文件中定义的类型冲突。

采纳答案by Duncan Jones

I receive the error: The type Iterator is not generic; it cannot be parameterized with arguments

Eclipse tells me that the import java.util.Iterator conflicts with a type defined in the same file.

我收到错误:迭代器类型不是通用的;它不能用参数参数化

Eclipse 告诉我导入 java.util.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:

我能得到这两个确切错误的唯一方法是调用我的 class Iterator。我想如果您正在编写一个关于迭代的小测试类,这将是一个容易犯的错误:

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.

解决方案:不要这样做。叫它别的东西。

As fun as it is to try and guess what your code looked like, had you posted an entire example in your question this would have been a shorted process. I'm not ruling out that there's another code sample that produces those errors, although I failed to find one with a bit of experimentation.

尽管尝试猜测您的代码看起来很有趣,但如果您在问题中发布了整个示例,这将是一个简短的过程。我不排除还有另一个代码示例会产生这些错误,尽管我没有通过一些实验找到一个。

回答by Nirav Prajapati

check your import statement proper it may be possible that you have already import iterator that comes from another packages first organize your imports delete those imports and add

检查您的导入语句是否正确,您可能已经导入了来自另一个包的迭代器首先组织您的导入删除这些导入并添加

import java.util.Iterator;

hope it will works for you

希望它对你有用