Easily Convert Arrays to ArrayLists using Java
Storing data in arrays is common in Java, but sometimes you may need to work with collections like ArrayLists. Creating an ArrayList from an array is a straightforward process.
Converting an Array to ArrayList
Consider an Element[] array contains Element objects:
Element[] array = {new Element(1), new Element(2), new Element(3)};
To convert this array into an ArrayList
ArrayList<Element> arrayList = new ArrayList<>(Arrays.asList(array));
Example Usage
Here's an example to illustrate the conversion:
// Create an Element array Element[] array = {new Element(1), new Element(2), new Element(3)}; // Convert the array to ArrayList ArrayList<Element> arrayList = new ArrayList<>(Arrays.asList(array)); // Iterate over the ArrayList for (Element element : arrayList) { System.out.println(element); }
The above is the detailed content of How to Easily Convert Java Arrays to ArrayLists?. For more information, please follow other related articles on the PHP Chinese website!