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?

3

5 Answers

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); } } } 
10
new ArrayList( Arrays.asList( new String[]{"abc", "def"} ) ); 
4

Using Collections#addAll()

String[] words = {"ace","boom","crew","dog","eon"}; List<String> arrayList = new ArrayList<>(); Collections.addAll(arrayList, words); 
4
String[] words= new String[]{"ace","boom","crew","dog","eon"}; List<String> wordList = Arrays.asList(words); 
2

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);