Question:
I have an array that is initialized like: Element[] array = {new Element(1), new Element(2), new Element(3)}; I would like to convert this array into an object of the ArrayList class: ArrayListarraylist = ???;
Answer:
new ArrayList(Arrays.asList(array))
Code:
import java.util.*; public class ArrayToArrayList { public static void main(String[] args) { String[] array = {new String("aaa"),new String("bbb"),new String("ccc")}; ArrayList<String> list = new ArrayList<String>(Arrays.asList(array)); System.out.println("array: " + Arrays.toString(array)); System.out.println("list: " + list); } }
Output:
$ java ArrayToArrayList array: [aaa, bbb, ccc] list: [aaa, bbb, ccc]