I came across this code snippet that solves it.
//Creating a sample ArrayList
List list = new ArrayList();
//Adding some long type values
list.add(100l);
list.add(200l);
list.add(300l);
//Converting the ArrayList to a Long
Long[] array = (Long[]) list.toArray(new Long[list.size()]);
//Printing the results
System.out.println(array[0] + " " + array[1] + " " + array[2]);
The conversion works as follows:
- It creates a new Long array, with the size of the original list
- It converts the original ArrayList to an array using the newly created one
- It casts that array into a Long array (Long[]), which I appropriately named 'array'