Assigning an array to an ArrayList in Java
Solution 1:
You can use Arrays.asList()
:
Type[] anArray = ...
ArrayList<Type> aList = new ArrayList<Type>(Arrays.asList(anArray));
or alternatively, Collections.addAll()
:
ArrayList<Type> aList = new ArrayList<Type>();
Collections.addAll(theList, anArray);
Note that you aren't technically assigning an array to a List (well, you can't do that), but I think this is the end result you are looking for.
Solution 2:
The Arrays
class contains an asList
method which you can use as follows:
String[] words = ...;
List<String> wordList = Arrays.asList(words);
Solution 3:
If you are importing or you have an array (of type string) in your code and you have to convert it into arraylist (offcourse string) then use of collections is better. like this:
String array1[] = getIntent().getExtras().getStringArray("key1"); or
String array1[] = ...
then
List<String> allEds = new ArrayList<String>();
Collections.addAll(allEds, array1);