Programming

How To Convert ArrayList To Array In Java?

How To Convert ArrayList To Array In Java?

Introduction

The elements of an ArrayList are objects belonging to wrapper classes such as Integer, Double, Boolean, and Short. These can store primitive values of one type. In contrast, array elements are primitive types. Examples are int, double, byte, and short.

Java provides several methods to change the data type of primitives to objects and vice versa, such as autoboxing (converting primitive values to corresponding objects of wrapper classes) and unboxing (converting objects of wrappers to primitive values).

The simplest way to convert an ArrayList to an Array is to create an array of the specified size and start adding numbers sequentially.

Converting ArrayLists to Arrays in Java

Here are four different ways to convert.

1. Using The Get() Method To Manually Convert

In the previous section, we discussed declaring an array of a given size, iterating over each number, and adding it to the array. You can do that in Java by following the code below.

Code

import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        List<Integer> arr_list = new ArrayList<Integer>();
        arr_list.add(1);
        arr_list.add(2);
        arr_list.add(3);
        arr_list.add(4);

        int n = arr_list.size();
        int[] PrimitiveArr = new int[n];
        for(int i=0; i<n; i++) {
            PrimitiveArr[i] = arr_list.get(i);
        }

        for(int i=0; i<n; i++) {
            System.out.println(PrimitiveArr[i] );
        }
    }
}
Output
1
2
3
4

write your code here: Coding Playground

2. Using toArray()

To convert an ArrayList to an Array in Java, use the toArray() method in the List interface, which returns an Object array. The method will convert the list elements into the array without disrupting their order.

The following Java program converts an ArrayList to an Object[] and iterates through the array content.

Sample Code

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayListToArray {
    public static void main(String[] args) {
        List<Integer> arr_list = new ArrayList<>();
        arr_list.add(1);
        arr_list.add(3);
        arr_list.add(5);
        arr_list.add(7);

        Integer[] Arr = null;

        Arr = arr_list.toArray(new Integer[arr_list.size()]);
        System.out.println(Arrays.toString(Arr));
    }
}
Output
[1, 3, 5, 7]

write your code here: Coding Playground

3. Using toArray(T[] a)

In Java, the toArray() method is used to change an ArrayList to an array whose type matches the parameter passed.

Sample Code

import java.io.*;
import java.util.List;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        List<Integer> arr_list = new ArrayList<Integer>();
        arr_list.add(1);
        arr_list.add(2);
        arr_list.add(3);
        arr_list.add(4);

        int n=arr_list.size();
        Integer[] arr1 = new Integer[n];
        arr1 = arr_list.toArray(arr1);

        for (int i=0; i<n; i++)
            System.out.println(arr1[i]);
    }

}

Output
1
2
3
4

write your code here: Coding Playground

4. Using Streams Api To Convert Primitive Integers To Arrays

With the streams() method and mapToInt() method, we can convert ArrayList<Integer> to an array of primitive data type int

Sample Code

import java.io.*;
import java.util.List;
import java.util.ArrayList;

public class sample {
public static void main(String[] args)
{
List<Integer> arr_list = new ArrayList<Integer>();
arr_list.add(1);
arr_list.add(3);
arr_list.add(5);
arr_list.add(7);

int[] arr = arr_list.stream().mapToInt(i -> i).toArray();

for (int x : arr)
System.out.print(x + " ");
}
}
Output
1 3 5 7 

write your code here: Coding Playground