例如:
javac Foo.java
Note: Foo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
例如:
javac Foo.java
Note: Foo.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
如果使用不带类型说明符的集合(例如,Arraylist()
而不是ArrayList<String>()
),Java 5及更高版本中会出现这种情况.这意味着编译器无法判断您是否以类型安全的方式使用集合,即使用generics.
要消除警告,只需具体说明您在集合中存储的对象类型.所以
List myList = new ArrayList();
使用
List<String> myList = new ArrayList<String>();
在Java7中,可以使用Type Inference缩短泛型实例化.
List<String> myList = new ArrayList<>();