2 Replies Latest reply: Mar 23, 2008 4:14 PM by 807601 RSS

    List to Array conversion Type error (utterly confused)

      Hi all, this is my first post and as such I am new to the java language. I have googled my query to death and found nothing.

      I have a list and an array declared by
      List fileList = new ArrayList();
      public String [] filePaths, fTypes = null;
      then a recursive function populates the list with Strings
           private void getFileList(File root, List result) {
                if(root.isDirectory()) {
                     File [] child = root.listFiles(filter);
                    if(child!=null && child.length>0) {
                         for(int i=0; i<child.length; i++)
                              getFileList(child, result);
                } else {
      the problem is when I then try to convert the list into an array I have tried 
      filePaths = fileList.toArray(new String[fileList.size()]);
      filePaths = (String[])fileList.toArray(new String[fileList.size()]);
      filePaths = fileList.toArray();
      and I either get cannot convert String[] to Object[] or Exception in thread "main" java.lang.ArrayStoreException
           at java.lang.System.arraycopy(Native Method)
           at java.util.ArrayList.toArray(ArrayList.java:306)
           at search.<init>(search.java:11)
           at sand.main(sand.java:4)
      can anybody tell me what I am doing wrong?