簡體   English   中英

-Xlint:在netbeans中未選中

[英]-Xlint:unchecked in 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 Generics。 舊的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 ,而無需進行顯式轉換。

泛型參數指示存儲在fileList中的任何內容必須是“至少”FileDescription。 編譯器檢查是否無法在列表中放置非FileDescription項,並且輸出代碼實際上不執行任何運行時檢查。 因此,泛型實際上不會像其他語言中的類似技術那樣遭受性能命中,但是編譯器執行的“類型擦除”使得諸如通用參數的運行時類型分析之類的技術難以實現。

嘗試一下,你會喜歡它們。

如果此代碼是在Generics發布之前編寫的,您可能希望使用向后兼容性標志(-source 1.4 -target 1.4)對其進行編譯。

您必須在列表中使用類型:

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

或者使用jdk7和diamond操作符,您可以使用:

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

所以在你的代碼中使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM