How to convert list to int array in Java

In this Java tutorial, we will learn how to convert list to int array in Java. In order to make it understandable and simple, we will create a list and some elements in it. Then we will convert list to integer array in Java. An easy example is also provided. Hope it will be helpful to the learners.

Convert list to int array in Java with an easy example

Let’s start with the algorithm first:

  1. Create a list<integer>
  2. Add some elements in list
  3. Create an integer array
  4. Define the size. ( size should be the same as the list, created first )
  5. Add each element of the list one by one to the newly created integer array using a for loop

You can also learn,

How to convert string array to int array in java with an example

How To Convert An ArrayList to Array In Java

Java Program to convert list to integer array in Java

import java.util.*;
public class MyClass {
   public static void main(String[] args) {
      List<Integer> my_list = new ArrayList<>();
      my_list.add(new Integer(15));
      my_list.add(new Integer(21));
      my_list.add(new Integer(3));
      my_list.add(new Integer(48));
      int list_length= my_list.size();
      int []  integer_array = new int [list_length];
      for(int i=0;i<list_length;i++) {
         integer_array[i] = my_list.get(i);
         System.out.println(integer_array[i]);
      }
     
   }
}

Output:

15
21
3
48

So you can see how list is converted to int[] array in Java. If you have any question, you can directly ask that in the comment section provided below.



How Java objects are stored in memory

Leave a Reply

Your email address will not be published. Required fields are marked *