How to convert Integer array list to integer array in Java?

Java 8Object Oriented ProgrammingProgramming

To convert integer array list to integer array is not a tedious task. First, create an integer array list and add some elements to it −

ArrayList < Integer > arrList = new ArrayList < Integer > ();
arrList.add(100);
arrList.add(200);
arrList.add(300);
arrList.add(400);
arrList.add(500);

Now, assign each value of the integer array list to integer array. We used size() to get the size of the integer array list and placed the same size to the newly created integer array −

final int[] arr = new int[arrList.size()];
int index = 0;
for (final Integer value: arrList) {
   arr[index++] = value;
}

Example

 Live Demo

import java.util.ArrayList;
public class Demo {
   public static void main(String[] args) {
      ArrayList<Integer>arrList = new ArrayList<Integer>();
      arrList.add(100);
      arrList.add(200);
      arrList.add(300);
      arrList.add(400);
      arrList.add(500);
      arrList.add(600);
      arrList.add(700);
      arrList.add(800);
      arrList.add(900);
      arrList.add(1000);
      final int[] arr = new int[arrList.size()];
      int index = 0;
      for (final Integer value : arrList) {
         arr[index++] = value;
      }
      System.out.println("Elements of int array...");
      for (Integer i : arr) {
         System.out.println(i);
      }
   }
}

Output

Elements of int array...
100
200
300
400
500
600
700
800
900
1000
raja
Published on 23-Apr-2019 12:13:33
Advertisements