We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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);
Answers
a File is not a String, that's what the error is saying
which do you want?
Use File::list() in place of File::listFiles(): L-)
https://Docs.Oracle.com/javase/9/docs/api/java/io/File.html#list--
Or better yet, go w/ PApplet::listPaths() instead: \m/
https://Forum.Processing.org/two/discussions/tagged?Tag=listpaths()
Hi, @koogs I have an array obtained by:
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.
so you do want Strings. so use the list() method gotoloop mentioned. only you can't store that in a File[]:
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]
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.
no, because it's a List of Files, not a List of Strings like it's expecting:
don't use a File array, or a File list.
a File is not a String.
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~
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.
not helpful. what is the error?
runnable example, in java, not for android
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
Another thing to be aware of is that Arrays::asList() returns a fixed-size List: L-)
https://Docs.Oracle.com/javase/9/docs/api/java/util/Arrays.html#asList-T...-
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) );
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)
I have it working now.
https://GitHub.com/ketai/ketai/pull/61