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

前端 未结 2 2032
栀梦
栀梦 2021-01-22 00:31

Given this Code

import java.util.Iterator;

private static List someList = new ArrayList();

public static void main(Strin         


        
相关标签:
2条回答
  • 2021-01-22 01:01

    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.

    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.

    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.

    0 讨论(0)
  • 2021-01-22 01:12

    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

    0 讨论(0)
提交回复
热议问题