java “ArrayList 类型不是通用的”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10557102/
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
What does "The type ArrayList is not generic" mean?
提问by John Hoffman
I am new to Java and trying to learn the various collections that programmers can use. I imported "java.util" into a scrapbook in Eclipse and inspected the following code.
我是 Java 新手,正在尝试学习程序员可以使用的各种集合。我将“java.util”导入 Eclipse 中的剪贴簿并检查了以下代码。
ArrayList<String> list = new ArrayList<String>();
list.add("test1");
list.add("test2");
I receive this output.
我收到这个输出。
The type ArrayList is not generic; it cannot be parameterized with arguments <String>
Syntax error, parameterized types are only available if source level is 5.0
The type ArrayList is not generic; it cannot be parameterized with arguments <String>
Syntax error, parameterized types are only available if source level is 5.0
What does this error mean? I did not make a generic array list; I made an array list of strings. Furthermore, what is "source level"?
这个错误是什么意思?我没有制作通用数组列表;我做了一个字符串数组列表。此外,什么是“源级别”?
回答by mprabhat
Your Java version in Eclipse is set to 1.4, generics in java were introduced only in Java 5.
您在 Eclipse 中的 Java 版本设置为 1.4,Java 中的泛型仅在 Java 5 中引入。
Change your JDK to 1.5 or above in eclipse that will resolve this issue.
在 Eclipse 中将您的 JDK 更改为 1.5 或更高版本即可解决此问题。
You can check your JDK by Project - > Java Build Path - > Libraries
您可以通过以下方式检查您的JDK Project - > Java Build Path - > Libraries
If here you see it being Java 1.5 or above then check the compiler Compliance is set to 5 and above.
如果在这里您看到它是 Java 1.5 或更高版本,请检查编译器合规性是否设置为 5 及更高版本。
You can check that Project - > Java Compiler
你可以检查一下 Project - > Java Compiler
EDIT:
编辑:
To add new jdk to Eclipse
将新的 jdk 添加到 Eclipse
Right click on Project - > Java Build Path - > Libraries - > Add Libraries - > JRE System Library - > Installed Libraries - > Add - > Standard VM - > Provide your installation location and press OK
右键Project -> Java Build Path -> Libraries -> Add Libraries -> JRE System Library -> Installed Libraries -> Add -> Standard VM -> 提供你的安装位置,然后按OK
Note in the list of Installed JRE, ensure that you check your Java 7.
请注意在已安装 JRE 的列表中,确保检查您的 Java 7。
回答by Hyman
What comes into my mind:
我想到了什么:
- check if the JDK is fully compliant with generics (so that the
ArrayList
class in your JSE is actually a generic class) - check if you don't have another
ArrayList
which has scope precedence and overrides the standard library definition
- 检查 JDK 是否完全符合泛型(以便
ArrayList
您的 JSE中的类实际上是一个泛型类) - 检查您是否没有另一个
ArrayList
具有范围优先级并覆盖标准库定义的
回答by DragonZoned
Yes the problem goes away for 1.5 and above. It feels like the question is not completely addressed though, I'm adding my 2 cents in case anybody comes across this question. It's mainly about this part of the question:
是的,1.5 及以上版本问题消失了。感觉这个问题还没有完全解决,如果有人遇到这个问题,我会加上我的 2 美分。主要是关于这部分的问题:
I did not make a generic array list; I made an array list of strings.
我没有制作通用数组列表;我做了一个字符串数组列表。
The error message mentioned:
错误消息提到:
> The type ArrayList is not generic; it cannot be parameterizedwith arguments <String>
> Syntax error, parameterized typesare only available if source level is 5.0
> ArrayList 类型不是通用的;它不能被参数化用参数<字符串>
> 语法错误,参数化类型仅在源级别为 5.0 时可用
What it actually means is, since Java 1.5 we can use Type parameters also (where in one was used to using value parameters). JDK 1.5 introduces generics, which allows us to abstract over types (or parameterized types).
它的实际含义是,从 Java 1.5 开始,我们也可以使用类型参数(其中一个用于使用值参数)。JDK 1.5 引入了泛型,它允许我们对类型(或参数化类型)进行抽象。
The class designers can be genericabout types in the definition. The arrayList implementation would be as below:
类设计器可以对定义中的类型通用。arrayList 实现如下:
public class ArrayList<E> implements List<E> .... {
// Constructor
public ArrayList() {...}
// Public methods
public boolean add(E e) {...}
public void add(int index, E element) {...}
public boolean addAll(int index, Collection<? extends E> c) {...}
public abstract E get(int index) {...}
public E remove(int index) {...}
...
}
Where Ecan be anytype like String or Integer etc. Hence the name genericarrayList.
其中E可以是任何类型,如 String 或 Integer 等。因此名称为通用arrayList。
The users can be specific in the types during the object instantiation or method invocation which was done in this example like below:
在此示例中完成的对象实例化或方法调用期间,用户可以特定于类型,如下所示:
ArrayList<String> list = new ArrayList<String>();
(Which was the confusion in above case, if I am not wrong :-))
(如果我没记错的话,这是上述情况的混淆:-))
Example of the usage of generics (if needed):
泛型的使用示例(如果需要):
// Declaring a DAO layer
public interface IMasterAbstractDao<E, I> {
public E findById(I id) {...}
public void delete(E e) {...}
public List<E> findByCriteria(Criterion criterion) {...}
}
Where Eis the entity type returned. This can be used for all the Model beansdefined in the system making it generic.
其中E是返回的实体类型。这可以用于系统中定义的所有模型bean,使其成为通用的。
Hope this helps.
希望这可以帮助。