2013-02-28 16 views
6

Bir "normal" Dizi Dosyasına [] bir ArrayList oluşturmalıyım.Dizi Listesi <File> Array Dosyasına []

File[] fSorted = (File[]) x.toArray(); 

Hata: Cast Exception

Exception in thread "Thread-5" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.io.File; 

nasıl File [] -List listeyi x dönebilirim?

-

Benim fonksiyonu:

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; 

} 
+1

olası yinelenen ([Dize \ [\] ile ArrayList dönüştürme] http://stackoverflow.com/questions/5374311/convert-arrayliststring-to-string) – Eric

+1

olası kopyası [java'daki diziye dönüştürme listesi] (http://stackoverflow.com/questions/9572795/convert-list-to-array-in-java) –

cevap

20

Kullanım: ait

File[] fSorted = x.toArray(new File[x.size()]); 
+0

Bu çalıştı. Teşekkür ederim! – bbholzbb

+2

+1 Kendim, Ben Foo [] dizi = yeni Foo [list.size()]; list.toArray (array); 'sadece okunabilirlik için :) –

İlgili konular