java 为什么 ArrayList 导入不起作用?(爪哇)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26430015/
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
Why is the ArrayList import not working? (Java)
提问by Hymana92
The following code will not compile, apparently because "the import conflicts with a type defined in the same file." Why won't this work?!
以下代码将无法编译,显然是因为“导入与同一文件中定义的类型冲突”。为什么这行不通?!
import java.util.ArrayList;
public class ArrayList {
public static void main(String[] args) {
ArrayList<Integer> aList = new ArrayList<Integer>();
}
}
回答by Ryan E
Just rename your class to something else, like MyArrayList
只需将您的类重命名为其他名称,例如 MyArrayList
回答by Dave Gillie
ArrayList is defined in java.util.ArrayList. The error message is telling you that your class name is conflicting with that definition.
ArrayList 在 java.util.ArrayList 中定义。错误消息告诉您您的类名与该定义冲突。
This won't do anything except create a list, but this will fix your compilation problem..
除了创建列表之外,这不会做任何事情,但这将解决您的编译问题..
public class MyListTest { // <---- This line cannot say "public class ArrayList"
public static void main(String [] args) {
ArrayList<Integer> aList = new ArrayList<Integer>();
}
}