Java 类型 Collection 不是通用的;它不能用参数 <? 扩展 E>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1804287/
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
The type Collection is not generic; it cannot be parameterized with arguments <? extends E>
提问by dbf
I have a strange problem with Eclipse Galileo.
I set Java 1.6 as my JRE. On this line of code
我对 Eclipse Galileo 有一个奇怪的问题。
我将 Java 1.6 设置为我的 JRE。在这行代码
List templates = new ArrayList ();
I see the following error in Eclipse's problem list:
我在 Eclipse 的问题列表中看到以下错误:
The type Collection is not generic; it cannot be parameterized with arguments
类型 Collection 不是通用的;它不能用参数参数化
I don't have any problems with building this project with Ant.
How can I fix it? Looks like it is an Eclipse problem, but because of this error, I can't compile/publish my project from the IDE.
我用 Ant 构建这个项目没有任何问题。
我该如何解决?看起来这是一个 Eclipse 问题,但由于这个错误,我无法从 IDE 编译/发布我的项目。
采纳答案by dbf
For those, who will get there from Google: the problem was with cryptix library. When I removed it from java build path the project is compiled sucesfully.
对于那些将从谷歌到达那里的人:问题出在 cryptix 库上。当我从 java 构建路径中删除它时,项目编译成功。
回答by VonC
What List
are you importing? (see this thread from 2006)
java.awt.List
or java.util.List
?
java.awt.List
或者java.util.List
?
Because, as eclipse aptly comments, java.awt.List
is not parameterized ;)
因为,正如 eclipse 恰当地评论的那样,java.awt.List
没有参数化;)
Check also the
还检查
- Java Build path: it must not contain a reference to the J2SE 1.4.2 libraries.
- Source Compatibility: project properties -> Java Compiler Settings, Source Compatibility 5.0 or 6.0.
Other than that, there was lots of issue back in 2005when the latest Eclipse 3.1 beta was supporting J2SE5, but this was fixed since then.
除此之外,在 2005 年最新的 Eclipse 3.1 beta 支持 J2SE5 时存在很多问题,但此后已修复。
Try tyo use the latest JDK6 in your project.
尝试在您的项目中使用最新的 JDK6。
回答by Andreas Dolk
Sometimes it's an eclipse hiccup and eclipse -clean
plus refreshing all projects helps.
有时它是一个日食小问题,eclipse -clean
加上刷新所有项目会有所帮助。
Edit
编辑
Does it change anything when you replace your code with:
当您将代码替换为以下内容时,它是否会改变任何内容:
java.util.List templates = new java.util.ArrayList();
or even
甚至
java.util.List<Object> templates = new java.util.ArrayList<Object>();
?
?
回答by user85421
Some ideas:
一些想法:
- check the JRE library being used in your project (check the package explorer).
- check the installed JREs in the eclipse settings (same as used by ant).
- comment out the line just to check if it really is the error cause.
- retype the whole line from scratch.
- install a new (clean) version of eclipse, in a new folder (testing).
- 检查项目中使用的 JRE 库(检查包资源管理器)。
- 在 eclipse 设置中检查已安装的 JRE(与 ant 使用的相同)。
- 注释掉该行只是为了检查它是否真的是错误原因。
- 从头开始重新输入整行。
- 在新文件夹(测试)中安装新(干净)版本的 eclipse。
回答by Fabio
put the entry "JRE System Library..." at the top in project, properties, java build path, order and export
将条目“JRE 系统库...”放在项目、属性、java 构建路径、顺序和导出的顶部
回答by Camilo Cuesta
Hey, I removed the cryptic library and it didn't work. But then I put JRE System Library at the top, and it worked. Really weird.
嘿,我删除了神秘库,但它不起作用。但后来我把 JRE 系统库放在了最上面,它奏效了。真的很奇怪。
回答by Sankar R.K
make java buildpath reference to greater than or equal to java 1.5
使 java buildpath 引用大于或等于 java 1.5
or you try to add the "import java.util.List" statement then you can see that
或者您尝试添加“import java.util.List”语句,然后您可以看到
eclipse is saying it is conflicting with some other List type
eclipse 说它与其他一些 List 类型冲突
for example it may be conflicting with com.lowagie.xx.xxx.List etc try to avoid these import
例如它可能与 com.lowagie.xx.xxx.List 等冲突,尽量避免这些导入
statements
声明
回答by Preston Cummings
Did you name your class list? i.e:
你有没有说出你的班级名单?IE:
import java.util.*;
public class List { // can't do this, name this something else.
public static void main(String[] args) {
List<Integer> l = new ArrayList<Integer>();
}
}
回答by xxxx
use "import java.util.List"
使用“导入 java.util.List”
instead of default import "import antlr.collections.List;"
而不是默认导入“import antlr.collections.List;”
and use the JRE5 or above for generic support of collection API....
并使用 JRE5 或更高版本对集合 API 的通用支持....
回答by zen0n
for example:
例如:
public class AClass<T extends Object>
{
public HashMap<String, String> myMap;
}
if I write:
如果我写:
public class BClass
{
private AClass aClass = new AClass();
public void test()
{
aClass.get("test");
//return Object class
}
}
but if I write:
但如果我写:
public class BClass
{
private AClass<?,?> aClass = new AClass<?,?>();
public void test()
{
aClass.get("test");
//return String class
}
}