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

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 06-Feb-2018 11:57:18
Advertisements