-Xlint:在netbeans中未选中



清洁&在NetBeans中构建我的项目时,有一个警告说"不安全的操作",所以我使用-Xlint:unchecked来查看这些操作,但我不明白我做错了什么。这些是警告,然后我的代码谢谢!

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
                }
            }
        }
    }

欢呼

您需要了解Java泛型。旧的1.4样式仍将编译,但会出现警告(有些人认为是错误)。

由于您使用的类需要Generic Type参数,因此需要将它们指定给编译器,如下所示:

 //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,而不需要执行显式强制转换。

generics参数表示存储在fileList中的内容必须"至少"是FileDescription。编译器检查是否不可能在列表中放置非FileDescription项,并且输出代码实际上不进行任何运行时检查。因此,泛型实际上不会像其他语言中的类似技术那样受到性能打击,但是编译器执行的"类型擦除"使某些技术(如泛型参数的运行时类型分析)变得不可能。

试试看,你会喜欢的。

如果这段代码是在Generics发布之前编写的,您可能希望使用向后兼容性标志(-source 1.4-target 1.4)对其进行编译

您必须在列表中使用类型,如:

List<Object> l = new ArrayList<Object>();

或者与jdk7和钻石运营商你可以使用:

List<Object> l = new ArrayList<>();

所以在你的代码使用:

private final List <FileDescription> fileList = Collections.synchronizedList(new ArrayList<FileDescription>());

相关内容

  • 没有找到相关文章

最新更新