To create an ArrayList from an array in Java, you can use the Arrays.asList method along with the new ArrayList constructor, like this:
String[] array = {"a", "b", "c"}; List<String> list = new ArrayList<>(Arrays.asList(array));
This creates a new ArrayList
from the elements of the array
using the Arrays.asList
method, and then constructs a new ArrayList
using the elements of the returned list.
Alternatively, you can use the Collections.addAll
method to add the elements of the array to an ArrayList
:
String[] array = {"a", "b", "c"}; List<String> list = new ArrayList<>(); Collections.addAll(list, array);
This creates a new empty ArrayList
, and then adds the elements of the array
to it using the Collections.addAll
method.