How to convert List<Integer> to int[] in Java?



You can simply iterate through list and fill the array as shown below −

import java.util.ArrayList; import java.util.List; public class Tester {    public static void main(String[] args) {       List<Integer> list = new ArrayList<>();       list.add(new Integer(1));       list.add(new Integer(2));       list.add(new Integer(3));       list.add(new Integer(4));       int[] array = new int[list.size()];       for(int i=0;i<array.length;i++) {          array[i] = list.get(i);          System.out.println(array[i]);       } } }

Output

1 2 3 4
Updated on: 2019-07-30T22:30:21+05:30

972 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements