Java ArrayList to Array – Convert ArrayList to Array

In this Java tutorial, we will learn- How to convert a Java ArrayList to Array of different types. In Java, ArrayList is a class, while Array is an object of a dynamically generated class.

In simple words-

Using an array, we can store limited primitive values or objects in an array, while, if we use ArrayList in Java, we can store unlimited primitive and non-primitive values. So, Let’s understand, How to convert Java ArrayList into an Array –

ArrayList to Array conversion in Java

The ArrayList class is present inside the Collection Framework of Java. ArrayList class is present inside the java.util package. Let’s say, we have done the processing part with an ArrayList, and now we need all information in the form of an Array.

So, to convert ArrayList into an Array, first, we need to import ArrayList from the java.util package, and we can use the method  .toArray( p[ ] yourReferenceVariable ) to convert it into an array. The method  toArray() returns an array containing all of the elements in this ArrayList from first to the last element. Here’s a quick solution –

Convert Java ArrayList to Array :

import java.util.*;

public class ArrayListToArray{
    public static void main(String[] args) {

        // Creating an ArrayList object - 
        ArrayList < String > theArrList = new ArrayList < String > ();

        // Working with ArrayList -
        theArrList.add("S");
        theArrList.add("h");
        theArrList.add("u");
        theArrList.add("b");
        theArrList.add("h");
        theArrList.add("a");
        theArrList.add("m");
        theArrList.add("K");
        theArrList.add("L");
        theArrList.add("o");
        theArrList.add("g");
        theArrList.add("i");
        theArrList.add("c");


        // Converting ArrayList into the Array of Strings -
        String[] theElementsArray = theArrList.toArray(new String[theArrList.size()]);

        // Printing the Array Values -
        System.out.println(Arrays.toString(theElementsArray));
        System.out.println("Array.length = " + theElementsArray.length);
    }
}

 

Output :

[S, h, u, b, h, a, m, K, L, o, g, i, c]
Array.length = 13

Method Used to convert ArrayList into Array :

toArray ( ) : Returns Object[ ]

It returns an array containing all of the elements in this list in an ordered manner (i.e. from first to last element). The returned array will be “safe” in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller can easily modify the returned array. This method acts as the bridge between Collection-based and Array-based APIs.

toArray ( TheTemplate[] T) : Returns TheTemplate[ ]

This version of toArray() method Returns an array containing all of the elements in this list in an ordered manner (i.e. from first to last element); The runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)


Wrap Up 

In this Java tutorial, we have learned how to convert Java ArrayList to an Array. There are majorly 2 types toArray() methods. One returns an array of Objects, the other one returns the type you specify.

Hope you found this tutorial helpful. For other popular related Core Java Tutorials, do check the below guides –

1) String split() complete guide.
2) Top 4 ways to generate Random numbers.
3) Fastest way to generate Prime numbers
4) Generate Fibonacci series in Java
5) 7 Different ways to Write in File with Java
6) Use of volatile in Multi-threading Environment
7) Find nth Prime number- Java
8) Complete Core Java Concepts

Leave a Comment