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

前端 未结 2 2061
栀梦
栀梦 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 someList = new ArrayList();
    
        public static void main(String[] args) {
            someList.add("monkey");
            someList.add("donkey");
    
            for (Iterator 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.

提交回复
热议问题