Convert ArrayList to Int Array in Java

Joel Swapnil Singh Feb 06, 2022 Jan 06, 2022
  1. Need to Convert an Arraylist to an Int Array in Java
  2. Ways to Convert an Arraylist to an Int Array in Java
Convert ArrayList to Int Array in Java

We can store both primitive data types (data types with a defined size and include data of similar types such as byte, char, short, int, etc.) and class objects in an array.

Whereas we can store only class objects in an ArrayList. Also, an ArrayList has a dynamic length that can increase or decrease according to the number of elements.

In this article, we will discuss different ways to convert an array list to an int array in Java.

Need to Convert an Arraylist to an Int Array in Java

We can perform all the operations done using an array with the help of an ArrayList.

But an ArrayList is comparatively slow in the execution of different operations. This is because an ArrayList has a collection of actions like get(), put(), resize(), and many others, all of which affect CPU time and memory utilization.

If you need to perform some actions only on fixed-size data, you must always use Arrays instead of ArrayList.

Ways to Convert an Arraylist to an Int Array in Java

Suppose we are given an ArrayList of integers. We need to convert the ArrayList into an int Array. We can do this using a for loop, Object[] toArray() method, T[] toArray() method, and mapToInt() method.

Let us discuss each of the approaches one by one.

Conversion of Arraylist to Int Array Using a for Loop

We can manually convert the ArrayList to an int array using a for-loop. If we are given an ArrayList, we will first declare an int array of the same size as the ArrayList.

We can use the size() function to calculate the ArrayList size. We can now copy the data elements from the ArrayList into the int array.

For this, we will use the get() method and the for loop to traverse the ArrayList elements. We have implemented this approach below in the solve() function.

Let us take a look at the code below to understand how it works.

import java.io.*;
import java.util.List;
import java.util.ArrayList;
public class Main 
{
    public static void solve(List<Integer> nums)
    {
      int[] arr=new int[nums.size()];
      for(int i=0;i<nums.size();i++)
      {
        arr[i]=nums.get(i);  
      }
      for (int i=0;i<arr.length;i++)
      {
        System.out.println(arr[i]);
      }
    }
    public static void main(String[] args)
    {
        List<Integer> nums = new ArrayList<Integer>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);
        System.out.println("ArrayList elements");
        for (int i=0;i<nums.size();i++)
        {
            System.out.println(nums.get(i));    
        }
        System.out.println("Array elements");
        solve(nums); // ArrayList to Array Conversion
    }
}

Output:

ArrayList elements
10
20
30
40
50
Array elements
10
20
30
40
50

Conversion of Arraylist to Int Array Using Object[] toArray() Function

We can also use the Object[] toArray() function to convert an ArrayList to int array in Java.

If given an ArrayList, we will first build an Object object and use the toArray() function. Keep in mind that the toArray() method returns an array of the Object type.

To eliminate this, we must typecast it into the desired data type (in this case, int) to avoid compilation errors. We have implemented this approach below in the solve() function.

Let us take a look at the code below to understand how it works.

import java.io.*;
import java.util.List;
import java.util.ArrayList;
public class Main 
{
    public static void solve(List<Integer> nums)
    {
      Object[] obj_arr= nums.toArray();
      int[] arr=new int[nums.size()];
      for(int i=0;i<obj_arr.length;i++)
      {
        arr[i]=(int)obj_arr[i];
      }
      for (int i=0;i<arr.length;i++)
      {
        System.out.println(arr[i]);
      }
    }
    public static void main(String[] args)
    {
        List<Integer> nums = new ArrayList<Integer>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);
        System.out.println("ArrayList elements");
        for (int i=0;i<nums.size();i++)
        {
            System.out.println(nums.get(i));    
        }
        System.out.println("Array elements");
        solve(nums); // ArrayList to Array Conversion
    }
}

Output:

ArrayList elements
10
20
30
40
50
Array elements
10
20
30
40
50

Conversion of Arraylist to Int Array Using T[] toArray Function

We can also use the T[] toArray() function to convert an ArrayList to int array in Java. If given an ArrayList, we will first build an Integer object and use the toArray() function.

In this method, if the array is not big enough, then a new array of the same type is allocated for this purpose. We have implemented this approach below in the solve() function.

Let us take a look at the code below to understand how it works.

import java.io.*;
import java.util.List;
import java.util.ArrayList;
public class Main 
{
    public static void solve(List<Integer> nums)
    {
      Integer[] int_arr=new Integer[nums.size()];
      int_arr=nums.toArray(int_arr);
      int[] arr=new int[nums.size()];
      for(int i=0;i<int_arr.length;i++)
      {
        arr[i]=int_arr[i];
      }
      for (int i=0;i<arr.length;i++)
      {
        System.out.println(arr[i]);
      }
    }
    public static void main(String[] args)
    {
        List<Integer> nums = new ArrayList<Integer>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);
        System.out.println("ArrayList elements");
        for (int i=0;i<nums.size();i++)
        {
            System.out.println(nums.get(i));    
        }
        System.out.println("Array elements");
        solve(nums); // ArrayList to Array Conversion
    }
}

Output:

ArrayList elements
10
20
30
40
50
Array elements
10
20
30
40
50

Conversion of Arraylist to Int Array Using mapToInt() Function

We can also convert an ArrayList to an int array in Java by combining the streams() method from the list with the mapToInt() function, which converts the data in the ArrayList to primitive integer values. This is a new and fascinating method for converting an ArrayList to an integer array that is accessible starting with Java 8.

If given an ArrayList, we will first call the stream() function on the provided ArrayList. After that, we will invoke the mapToInt() method, which returns an integer stream consisting of results.

And after that, we will call the toArray() method, which will convert the extracted results into an int array. We have implemented this approach below in the solve() function.

Let us take a look at the code below to understand how it works.

import java.io.*;
import java.util.List;
import java.util.ArrayList;
public class Main 
{
    public static void solve(List<Integer> nums)
    {
      int[] arr = nums.stream().mapToInt(i -> i).toArray();
      for (int i=0;i<arr.length;i++)
      {
        System.out.println(arr[i]);
      }
    }
    public static void main(String[] args)
    {
        List<Integer> nums = new ArrayList<Integer>();
        nums.add(10);
        nums.add(20);
        nums.add(30);
        nums.add(40);
        nums.add(50);
        System.out.println("ArrayList elements");
        for (int i=0;i<nums.size();i++)
        {
            System.out.println(nums.get(i));    
        }
        System.out.println("Array elements");
        solve(nums); // ArrayList to Array Conversion
    }
}

Output:

ArrayList elements
10
20
30
40
50
Array elements
10
20
30
40
50

Related Article - Java Array

Related Article - Java ArrayList