How can I convert List<Integer> to int[] in Java?
You can use the toArray()
method of the List
interface to convert a List<Integer>
to an int[]
in Java. Here's an example:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int[] array = list.stream().mapToInt(i -> i).toArray();
Alternatively, you can use the List.toArray(T[] a)
method, which returns an array containing all of the elements in the list in proper sequence (from first to last element). Here's an example:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int[] array = new int[list.size()];
array = list.toArray(array);
I hope this helps! Let me know if you have any other questions.