Converting File[] to ArrayList

edited March 2018 in Android Mode

Hi, I've tested in several ways this conversion and the most logical way, seems to me would be by iterating, but it gives the error:

The method add(String) in the type ArrayList is not applicable for the arguments (File)

How to do this, thanks.

ArrayList<String> alfiles = new ArrayList<String>();
File[] files = f.listFiles();
for (File each : files) alfiles.add(each);


  • a File is not a String, that's what the error is saying

    which do you want?

  • edited March 2018

    Use File::list() in place of File::listFiles(): L-)

    Or better yet, go w/ PApplet::listPaths() instead: \m/

  • Hi, @koogs I have an array obtained by:

    String path = "/storage/emulated/legacy/Download"; 
    File f = new File(path); 
    File[] files = f.listFiles();

    Now I want to convert it to an ArrayList to be used by the KetaiList Class

  • Hi, @GoToLoop. Using just list() gives me one more error namely

    Type mismatch: cannot convert from String[] to File[]

    I've read the document of your link and it is a string array, but of a different type (split by commas I think). I'm using it in android so for your second answer I don't think it's applicable.

  • edited March 2018

    so you do want Strings. so use the list() method gotoloop mentioned. only you can't store that in a File[]:

    String path = "/storage/emulated/legacy/Download"; 
    File f = new File(path); 
    String[] files = f.list();
  • it is a string array, but of a different type (split by commas I think)

    that's just the output from printArray()

  • koogs=== The outputs can be like

    [1] path/file1 [2]path/file2 etc.

    Or [path/file1 , path/file2]

  • edited March 2018

    When I use


    I get an output like

    [ path/file1, path/file2]

    And this is accepted by the KetaiList Class

    When I convert the File array with

    List< File >alfiles = Arrays.asList(files);

    I get a simular output [ path/file, etc], but this array is not accepted by the KetaiList Class

    PS I had to put a space between < File > , Otherwise it would just disappear in this comment section.

  • Answer ✓

    but this array is not exepted by the KetaiList Class

    no, because it's a List of Files, not a List of Strings like it's expecting:

    public KetaiList(PApplet _parent, ArrayList<String> data)
    public KetaiList(PApplet _parent, String[] data)

    don't use a File array, or a File list.

    a File is not a String.

  • edited March 2018 Answer ✓

    I don't have Android here. ^#(^

    But according to @koogs, 1 of the overloaded constructors of class KetaiList can accept a String[] array directly. B-): public KetaiList(PApplet _parent, String[] data)

    If that's so, here's my completely untested blind attempt: =P~

    final String[] files = listPaths("/storage/emulated/legacy/Download");
    final KetaiList klist = new KetaiList(this, files);
  • Yes, that's right, it accepts a String[] array directly also. Now it's working. I still would like to know how to convert a File[] array into an ArrayList. List< File >alfiles = Arrays.asList(files); does not work, but I guess this will be for another time. Thank you.

  • edited March 2018
    import java.util.List;
    import java.util.Arrays;
    final File[] files = new File("/storage/emulated/legacy/Download/").listFiles();
    final List<File> lfiles = Arrays.asList(files);
  • does not work

    not helpful. what is the error?

    runnable example, in java, not for android

    import java.util.Arrays;
    import java.util.List;
    File[] fileArray = new File[2];
    fileArray[0] = new File("/tmp/a");
    fileArray[1] = new File("/tmp/b");
    // File[] to List<File>
    List<File> fileList1 = Arrays.asList(fileArray);
    println("1", fileList1);
    // File[] to ArrayList<File>
    ArrayList<File> fileList2 = new ArrayList<File>(Arrays.asList(fileArray));
    println("2", fileList2);

    Arrays.asList returns a List, not an ArrayList so you have to define the class as a List, as per fileList1. and import List

    or use the Arrays.asList() to convert to a List, which you can then use in the ArrayList, constructor, as per fileList2

  • edited March 2018

    Another thing to be aware of is that Arrays::asList() returns a fixed-size List: L-)

    final List<File> lfiles = Arrays.asList(files);

    If dynamic-size is needed, pass it as an argument for the ArrayList's constructor: *-:)

    final List<File> lfiles = new ArrayList<File>( Arrays.asList(files) );

  • edited March 2018

    public KetaiList(PApplet _parent, ArrayList<String> data)

    I wonder why the 2nd parameter is an ArrayList rather than just List. :-O

    Any professional library worth its bacon would only request a List, never an ArrayList! :-@

  • @koogs== Your runnable example will print into the console, but like before will not be accepted by KetaiList (it just won't show up) and doesn't give any error.

  • @GoToLoop== The last snippet you gave just after my last post will compile, but then crashes with the error:

    java.lang.NullPointerException: storage == null

  • Well, we only have the code you've posted and we've answered that specific question, "Converting File[] to ArrayList". If you then go on to use the array / ArrayList you haven't shown us that, just vaguely mentioned some android library method you're using.

    Post more code for a better chance of a working solution. (But not from me - no android setup)

  • edited March 2018

    I have it working now.

    import ketai.ui.*;
    import android.os.Environment;
    KetaiList klist;
    String[] files;
    void setup() { 
      text("Click screen to open list.", width/2, height/2);
      String directory = "/Download";
      String path = Environment.getExternalStorageDirectory().toString() + directory;
      files = listPaths(path);
    void draw() {
    void mousePressed() { 
      klist = new KetaiList(this, files);
    void onKetaiListSelection(KetaiList klist) { 
      String selection = klist.getSelection(); 
      println("Your chosen file is:  " + selection);
Sign In or Register to comment.