Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Possible Duplicate:
Converting array to list in Java

I want to convert String array to ArrayList. For example String array is like:

String[] words = new String[]{"ace","boom","crew","dog","eon"};

How to convert this String array to ArrayList?

share|improve this question

marked as duplicate by Harry Joy, home, kostja, Marko Topolnik, abatishchev May 10 '12 at 13:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

17  
+1; it's now the second link on google. – Steve Kehlet Mar 22 '13 at 0:32
up vote 150 down vote accepted

Use this code for that,

import java.util.Arrays;  
import java.util.List;  
import java.util.ArrayList;  
public class StringArrayTest  
{  
   public static void main(String[] args)  
   {  
      String[] words = {"ace", "boom", "crew", "dog", "eon"};  

      List<String> wordList = Arrays.asList(words);  

      for (String e : wordList)  
      {  
         System.out.println(e);  
      }  
   }  
}
share|improve this answer
9  
In summary all you need is the line List<String> wordList = Arrays.asList(words); . Is this correct? – Keale Aug 5 '14 at 2:40
    
@Keale If words is an array of strings, then, yes. – Nicolai S Oct 22 '15 at 22:06
6  
Beware: Arrays.asList(...) creates an AbstractList, not a real list. So you can do stuff like wordList.sort((a, b) -> a.length() - b.length()), but you can not do wordList.remove(0), or add to it. This will throw UnsupportedOperationException. – Nicolai S Oct 22 '15 at 22:08
new ArrayList( Arrays.asList( new String[]{"abc", "def"} ) );
share|improve this answer
1  
i think that this solution is better because Arrays.asList return a java.util.Arrays.ArrayList.ArrayList<T>(T[]) so if you try to add something you'll get a java.lang.UnsupportedOperationException – Eomm Feb 19 '15 at 15:39
1  
If you have limited number of elemenets in the array, it can be written as new ArrayList( Arrays.asList("abc", "def")); – Diptopol Dam Mar 30 '15 at 3:46
    
To avoid IDE warnings, new ArrayList<>(...) worked for me as opposed to new ArrayList(...) (note the <>). – Farbod Salamat-Zadeh Aug 26 '15 at 14:31
1  
Best answer for me. With List<String> wordList = Arrays.asList(words);, doing wordList instanceof ArrayList<String> will return false. – alvgarvilla May 24 at 15:39
String[] words= new String[]{"ace","boom","crew","dog","eon"};
List<String> wordList = Arrays.asList(words);
share|improve this answer
3  
The result here is a List, but not an ArrayList. – Marko Topolnik May 10 '12 at 8:45
4  
@MarkoTopolnik you are right, this needs to be wrapped so List<String> wordList = new ArrayList<String>(Arrays.asList(words)); – Scorpion May 10 '12 at 9:24

Using Collections#addAll()

String[] words = {"ace","boom","crew","dog","eon"};
List<String> arrayList = new ArrayList<>(); 
Collections.addAll(arrayList, words); 
share|improve this answer
1  
I like this solution. It is concise and clean. – Dinesh Arora Aug 11 at 14:19

in most cases the List<String> should be enough. No need to create an ArrayList

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

...

String[] words={"ace","boom","crew","dog","eon"};
List<String> l = Arrays.<String>asList(words);

// if List<String> isnt specific enough:
ArrayList<String> al = new ArrayList<String>(l);
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.