我必須形成一個ArrayList到一個「正常」陣列文件[]。陣列列表<File>陣列文件[]
File[] fSorted = (File[]) x.toArray();
的錯誤:Cast Exception
Exception in thread "Thread-5" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.io.File;
我怎樣才能返回列表x
爲文件[] - 列出?
-
我的功能:
private File[] sortFiles(File[] files){;
ArrayList<String> allFiles = new ArrayList<String>() ;
for (int index = 0; index < files.length; index++)
{
File afile = new File(files[index].toString());
allFiles.add(afile.getPath().toString());
}
java.util.Collections.sort(allFiles);
ArrayList<File> x = new ArrayList<File>();
for(int i = 0; i< allFiles.size(); ++i){
x.add(new File(allFiles.get(i)));
}
File[] fSorted = (File[]) x.toArray();
return fSorted;
}
可能重複[轉換ArrayList的爲String \ [\](http://stackoverflow.com/questions/5374311/convert-arrayliststring-to-string) –
Eric
2013-02-28 17:33:40
可能的重複[Convert to list in java](http://stackoverflow.com/questions/9572795/convert-list-to-array-in-java) – 2013-02-28 17:41:25