我定义一个类成员如下:
private static List abc = Collections.synchronizedList(new ArrayList());
但是 Eclipse 给了我警告:
Type safety: Unchecked invocation synchronizedList(ArrayList) of the generic method synchronizedList(List<T>) of type Collections
是否有其他方法来编写此声明语句以避免警告?
最佳答案
您收到原始类型警告,因为您在使用 new ArrayList()
初始值设定项时未指定泛型类型。
自 Java 5 generics在Java中引入。
它们允许类型抽象和类型安全。
当您这样指定时,您的警告将会消失:
private static List<MyType> abc = Collections.synchronizedList(new ArrayList<MyType>());
...其中 MyType
是您想要该 List
的通用类型。
关于java - 使用synchronizedList时输入安全警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24494074/