Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add File [] Array content into ArrayList? [duplicate]

Tags:

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?

like image 693
vijayk Avatar asked May 08 '13 10:05

vijayk


People also ask

How do you add duplicate elements to an ArrayList in Java?

collect(Collectors. toList()); This code says for each member of the list turn it into 4 copies of the item then collect all those as a list and add them to the original list.

Can ArrayList add duplicate elements?

ArrayList allows duplicate values while HashSet doesn't allow duplicates values. Ordering : ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn't maintain any order.

How do you add a file to an ArrayList?

This Java code reads in each word and puts it into the ArrayList: Scanner s = new Scanner(new File("filepath")); ArrayList<String> list = new ArrayList<String>(); while (s. hasNext()){ list. add(s.

How do you copy an array to an ArrayList?

We can convert an array to arraylist using following ways. Using Arrays. asList() method - Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class.


2 Answers

Try this

tempList.addAll(Arrays.asList(temp)); 
like image 88
Sanjaya Liyanage Avatar answered Sep 21 '22 03:09

Sanjaya Liyanage


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)); 
like image 42
Adrian Shum Avatar answered Sep 22 '22 03:09

Adrian Shum