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?
35 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); } } } 10new ArrayList( Arrays.asList( new String[]{"abc", "def"} ) ); 4Using Collections#addAll()
String[] words = {"ace","boom","crew","dog","eon"}; List<String> arrayList = new ArrayList<>(); Collections.addAll(arrayList, words); 4String[] words= new String[]{"ace","boom","crew","dog","eon"}; List<String> wordList = Arrays.asList(words); 2in 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);