Home >Java >javaTutorial >How to Convert an Array to an ArrayList in Java?
Creating an ArrayList from an Array
Consider the following scenario:
Element[] array = {new Element(1), new Element(2), new Element(3)};
You wish to convert this array into an ArrayList of type
Solution:
To accomplish this conversion, utilize the Arrays.asList() method:
ArrayList<Element> arrayList = new ArrayList<>(Arrays.asList(array));
This method takes an array as input and returns a fixed-size list backed by the specified array. Modifications to the list will directly affect the elements of the original array.
The above is the detailed content of How to Convert an Array to an ArrayList in Java?. For more information, please follow other related articles on the PHP Chinese website!