title: Array List<File> to Array File[]
body: <p>I have to form an ArrayList to an "normal" Array File[].</p>
  
  <pre><code>File[] fSorted = (File[]) x.toArray();
  </code></pre>
  
  <p>The Error: <code>Cast Exception</code></p>
  
  <pre><code>Exception in thread "Thread-5" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.io.File;
  </code></pre>
  
  <p>How can I return the list <code>x</code> as File[ ]-List?</p>
  
  <p>--</p>
  
  <p>My function:</p>
  
  <pre><code>private File[] sortFiles(File[] files){;
  
      ArrayList&lt;String&gt; allFiles = new ArrayList&lt;String&gt;() ;
      for (int index = 0; index &lt; files.length; index++)  
      {  
          File afile = new File(files[index].toString());
          allFiles.add(afile.getPath().toString());
  
      } 
      java.util.Collections.sort(allFiles);
  
      ArrayList&lt;File&gt; x = new ArrayList&lt;File&gt;();
      for(int i = 0; i&lt; allFiles.size(); ++i){
          x.add(new File(allFiles.get(i)));
      }
      File[] fSorted = (File[]) x.toArray();
  
      return fSorted;
  
  }
  </code></pre>