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?

norbitrial
  • 14,716
  • 7
  • 32
  • 59
vijayk
  • 2,633
  • 14
  • 38
  • 59
  • loop through the `temp` array and add each File to the `tempList` – MaVRoSCy May 08 '13 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?) – Azurespot Apr 27 '15 at 01:07

5 Answers5

14

Try this

tempList.addAll(Arrays.asList(temp));
Rahul
  • 44,383
  • 11
  • 84
  • 103
Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50
  • 'http://stackoverflow.com/questions/16433915/how-to-copy-file-from-one-location-to-another-location' can you plz see this problem. – vijayk May 08 '13 at 10:26
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));
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
2

use following

List<File>tempList = Arrays.asList(temp);
ajduke
  • 4,991
  • 7
  • 36
  • 56
2

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

for (File each : temp)
  tempList.add(each);
The Cat
  • 2,375
  • 6
  • 25
  • 37
2

You can use a collections library call for this:

Arrays.asList(temp);
eldris
  • 205
  • 1
  • 5