Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

2  
-1; this could have easily be solved by just searching the internet. –  home May 10 '12 at 8:47
11  
+1; it's now the second link on google. –  Steve Kehlet Mar 22 '13 at 0:32

5 Answers 5

up vote 56 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
3  
In summary all you need is the line List<String> wordList = Arrays.asList(words); . Is this correct? –  Keale Aug 5 '14 at 2:40
new ArrayList( Arrays.asList( new String[]{"abc", "def"} ) );
share|improve this answer
    
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 yesterday
String[] words= new String[]{"ace","boom","crew","dog","eon"};
List<String> wordList = Arrays.asList(words);
share|improve this answer
2  
The result here is a List, but not an ArrayList. –  Marko Topolnik May 10 '12 at 8:45
3  
@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
List<String> wordList = new ArrayList<String>(); 
String[] words = {"ace","boom","crew","dog","eon"};
Collections.addAll(wordList, words); 
share|improve this answer
1  
this works all the time! –  Tech Junkie Oct 9 '14 at 8:02
    
Thanks for this! –  Tedboi Nov 1 '14 at 8:58

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.