Java List to array

Java List to array

Converting a List to an array using toArray()

List<Integer> longList = new ArrayList<Integer>();
 
longList.add(111);
longList.add(222);
longList.add(333);
longList.add(444);
longList.add(555);

//Printing the results
longList.forEach(System.out::println);

//Converting the ArrayList to an Integer
Integer[] array = (Integer[]) longList.toArray(new Integer[longList.size()]);

//Printing the results
for (Integer integer : array) {
	System.out.println(integer);
}