[英]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
). 好吧,首先,
Collection
是List
的超级接口,因此,除非您确定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.