简体   繁体   English

如何解决类型安全警告从集合到列表的未经检查的强制转换 <File> 没有抑制注释的Java 6

[英]How to address Type safety warning Unchecked cast from Collection to List<File> Java 6 without suppress annotation

I have the following code: 我有以下代码:

public void someMethod(String directory,final String extension) {

    List<File> fileList = (List<File>) FileUtils.listFiles(new File(directory), new String[] {extension} , true); 
    File[] files = new File[fileList.size()];
    // ...

    for(File f : files) {
       // looping...
    }
}

I get the yellow sqiggley line in my code with the warning: 我的代码中出现黄色的sqiggley行,并显示警告:

Type safety: Unchecked cast from Collection to List 类型安全性:从集合到列表的未经检查的演员表

How can I fix this? 我怎样才能解决这个问题?

Edit 1: 编辑1:

Please no suppression annotations, I'd like to understand what is happening. 请不要使用抑制注释,我想了解发生了什么。 I've tried doing this: 我尝试这样做:

if(fileList instanceof List<?>) {
    // data processing here...
}

this didn't make the warning go away...this is really bothering me... 这并没有使警告消失...这真的困扰我...

Edit 2: 编辑2:

I've decided to do this: 我决定这样做:

List<?> fileList = (List<?>) FileUtils.listFiles(new File(directory), new String[] {extension} , true);

File[] files = new File[fileList.size()];

int i=0;
if(fileList instanceof List<?>) {
    for (Object obj : fileList) {
        try {
            File file = (File)obj;
            files[i] = new File(file.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        i++;
    }
}

Well, first of all, Collection is a superinterface of List , so unless you are sure that your implementation of listFiles() will always return a List , you shouldn't downcast (so as to avoid a ClassCastException ). 好吧,首先, CollectionList的超级接口,因此,除非您确定listFiles()始终返回List ,否则您不应该向下转换(以避免ClassCastException )。 In most cases, if you need a list instead of a collection, it's wiser to simply create a new list from the returned collection, eg, 在大多数情况下,如果您需要列表而不是集合,则明智的做法是仅从返回的集合中创建一个新列表,例如,

Collection<File> myCollection = FileUtils.listFiles(...);
List<File> myList = new ArrayList<File>(myCollection);

Alternatively, if all you want to do is convert the collection into an array, there's no need to create any list at all. 另外,如果您要做的只是将集合转换为数组,则根本不需要创建任何列表。 All you have to do is: 您要做的就是:

Collection<File> myCollection = FileUtils.listFiles(...);
File[] myArray = myCollection.toArray(new File[myCollection.size()]);

If the compiler continues to give you warnings, then your only remaining problem lies with the listFiles() method itself; 如果编译器继续向您发出警告,则剩下的唯一问题在于listFiles()方法本身;否则,您可能会遇到其他问题。 it's returning the raw type Collection instead of the generic type Collection<File> . 它返回原始类型 Collection而不是通用类型 Collection<File> To remove the associated warning, either update to the latest version of Commons IO , or simply insert the @SuppressWarnings("unchecked") annotation appropriately, eg, 要删除相关的警告,请更新至Commons IO最新版本 ,或简单地适当插入@SuppressWarnings("unchecked")批注,例如,

@SuppressWarnings("unchecked")
Collection<File> myCollection = FileUtils.listFiles(...);

Why do you need a List<File> ? 为什么需要List<File> Why not do: Collection<File> files=FileUtils.listFiles(...) ? 为什么不这样做: Collection<File> files=FileUtils.listFiles(...) This won't give you a warning and you can still iterate over your Collection of files 这不会给您警告,您仍然可以遍历文件Collection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型安全:从对象到列表的未经检查的强制转换 - Type safety: Unchecked cast from Object to List 如何从Java中返回通用列表中删除“类型安全” /“未经检查的转换”警告? - How to remove 'Type Safety'/'Unchecked conversion' warning from returning generic List in Java? Java:输入安全性 - 未经检查的强制转换 - Java: Type safety - unchecked cast 警告:类型安全:未选中从Object到PK的转换 - Warning: Type safety: Unchecked cast from Object to PK 如何避免警告类型安全:未选中从Object到ArrayList的强制转换<Facility> - How to avoid warning Type safety: Unchecked cast from Object to ArrayList<Facility> 类型安全:未选中从列表到列表的强制转换<String> - Type safety: Unchecked cast from List to List<String> Java - 使用WatchEvent抑制未经检查的强制转换警告是否安全? - Java - Is it safe to suppress unchecked cast warning with WatchEvent? 类型安全:未经检查的从 WebElement 到 List 的强制转换<webelement></webelement> - Type safety: Unchecked cast from WebElement to List<WebElement> java 转换泛型类型,没有未经检查的转换警告 - java cast generic type without unchecked cast warning 类型安全性:未经检查的从Object到T(Java)的转换 - Type safety: Unchecked cast from Object to T (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM