22

I have an array:

File [] temp=null;

And I have an arrayList of File type:

List <File> tempList = new ArrayList <File>();

Now I want to add the content from temp to tempList. So anyone can please tell me How do I this?

2
  • loop through the temp array and add each File to the tempList Commented May 8, 2013 at 10:05
  • Just so others know, adding array contents to an ArrayList using a for loop will give you a warning in Android Studio that says it prefers you use the ArrayList's addAll() method. You could still do it, but for some reason Android Studio doesn't prefer it (maybe it's more efficient to use the method?) Commented Apr 27, 2015 at 1:07

5 Answers 5

14

Try this

tempList.addAll(Arrays.asList(temp));
Sign up to request clarification or add additional context in comments.

1 Comment

'stackoverflow.com/questions/16433915/…' can you plz see this problem.
4

If you are not going to update the content of the array (add/removing element), it can be as simple as

List<File> tempList = Arrays.asList(temp);

Of course, if you want a list that you can further manipulate, you can still do something like

List<File> tempList = new ArrayList<File>(Arrays.asList(temp));

2 Comments

The second line of code should be List<File> tempList = new ArrayList<>(Arrays.asList(temp));, note the diamond in ArrayList<>
Dunno why I missed that lol. Lemme fix that
2

use following

List<File>tempList = Arrays.asList(temp);

Comments

2

You can iterate through the array and add each element to the list.

for (File each : temp)
  tempList.add(each);

Comments

2

You can use a collections library call for this:

Arrays.asList(temp);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.