当在NetBeans中清理和构建我的项目时,有一个警告说“不安全的操作”,所以我使用NetBeans来查看这些操作,但是我无法理解我做错了什么。这些是警告,然后是我的代码,谢谢!
UploadBean.java:40: warning: [unchecked] unchecked conversion
private final List fileList = Collections.synchronizedList(new ArrayList());
required: List<T>
found: ArrayList
where T is a type-variable:
T extends Object declared in method <T>synchronizedList(List<T>)
UploadBean.java:40: warning: [unchecked] unchecked method invocation: method synchronizedList in class Collections is applied to given types
private final List fileList = Collections.synchronizedList(new ArrayList());
required: List<T>
found: ArrayList
where T is a type-variable:
T extends Object declared in method <T>synchronizedList(List<T>)
UploadBean.java:97: warning: [unchecked] unchecked call to add(E) as a member of the raw type List
fileList.add(fd);
where E is a type-variable:
E extends Object declared in interface List
3 warnings码
//This is line 40
private final List fileList = Collections.synchronizedList(new ArrayList());
//This is line 88
public void doUpload(FileEntryEvent e) {
FileEntry file = (FileEntry) e.getSource();
FileEntryResults result = file.getResults();
for (FileInfo fileInfo : result.getFiles()) {
if (fileInfo.isSaved()) {
FileDescription fd =
new FileDescription(
(FileInfo) fileInfo.clone(), getIdCounter());
synchronized (fileList) {
fileList.add(fd); //This is line 97
}
}
}
}干杯
发布于 2012-01-27 17:26:08
您需要了解。旧的1.4样式仍然会编译,但是它会使用警告(一些人认为是错误)来编译。
由于您使用的类是expect泛型类型参数,因此需要将它们指定给编译器,如下所示:
//This is line 40
private final List<FileDescription> fileList = Collections.synchronizedList(new ArrayList<FileDescription>());
//This is line 88
public void doUpload(FileEntryEvent e) {
FileEntry file = (FileEntry) e.getSource();
FileEntryResults result = file.getResults();
for (FileInfo fileInfo : result.getFiles()) {
if (fileInfo.isSaved()) {
FileDescription fd =
new FileDescription(
(FileInfo) fileInfo.clone(), getIdCounter());
synchronized (fileList) {
fileList.add(fd); //This is line 97
}
}
}
}请注意,对于泛型,某些类型的铸造不再必要。例如,fileList.get(0)将在上面的示例中返回一个FileDescription,而不需要进行显式的强制转换。
泛型参数表明,fileList中存储的任何内容都必须是“至少”一个FileDescription。编译器检查不可能将非FileDescription项放在列表中,并且输出代码实际上不执行任何运行时检查。因此,泛型实际上不会像其他语言中的类似技术那样受到性能影响,但是编译器预先形成的“类型擦除”使得某些技术(如泛型参数的运行时类型分析)不可能实现。
试试,你会喜欢的。
如果此代码是在Generics发布之前编写的,您可能希望使用向后兼容性标志(-source 1.4 -target 1.4)编译它。
发布于 2012-01-27 17:25:15
您必须在列表中使用类型,例如:
List<Object> l = new ArrayList<Object>();或者使用jdk7和菱形操作符,您可以使用:
List<Object> l = new ArrayList<>();因此,在您的代码中使用:
private final List <FileDescription> fileList = Collections.synchronizedList(new ArrayList<FileDescription>());https://stackoverflow.com/questions/9037206
复制相似问题