日期:2014-05-20  浏览次数:20871 次

JDK1.6.0对泛类的支持
List <String>   list   =   new   ArrayList <String> ();
在代码中提示这个出错,出错提示如:
Exception   in   thread   "main "   java.lang.Error:   Unresolved   compilation   problems:  
The   type   List   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

但eclipse用的是JDK1.6.0版本的,难道不支持?

------解决方案--------------------
sure!
import java.util.*;
try it:

class Test6 {
public static void main(String[] g) {
List list=new ArrayList <String> ();
list.add( "A ");

System.out.print(list);
}

}
------解决方案--------------------
你的项目的source level设置的是5.0以下的
所以不支持5.0才有的泛型

两种情况:
一种是Eclipse对所有项目默认的设置是5.0以下,项目继承了默认设置(在Preferences/Java/Compiler里改)
另一种是项目里设置了source level 5.0以下(在 项目属性/Java Compiler 里改)
------解决方案--------------------
关注