Conversion of ArrayList to Array Java Examples

In this article, we are learning the Conversion of ArrayList to Array java.

ArrayList is one of the most used class in Java Collections framework. Array is also one of the important data structure in almost every programming languages.

As a programmer, we often need to convert the ArrayList to Array in Java. So we are looking different ways of conversion of ArrayList to Array in this article of Java .

Let’s get started :

Introduction

There can be below mentioned ways for converting of ArrayList to Array in Java :

  • Using Object[] toArray() method.
  • Using T[] toArray(T[] a) method.
  • Using get() method / Deep copy of ArrayList.
Conversion of ArrayList to Array Java Example

Let’s explore the various ways with examples :

toArray() – to convert ArrayList to Array Java

The method toArray() exists in two forms as it is an overloaded method.

1. public Object[] toArray();

2. public <T> T[] toArray(T[] a);

Above mentioned first method does not have any parameters and this method returns an array of Object.

We have to iterate the objects array so that we can find the desired element and then the element can be typecasted to desired class type.

However, the second method has not any specific parameter type and return type. The runtime type for array to be returned is same as of the specified array.

T represents generic type in the above method.

Let’s understand about these methods in detail and see how to convert ArrayList into Array in Java with example :

Convert ArrayList to Array Java Example using – public Object[] toArray()

This method accepts none arguments and it is returning the array of objects therefore we are required to typecast the elements according to the desired type.

In the below Example , We are illustrating :

  • convert an ArrayList to Object Array
  • iterate through array content and typecast it.
import java.util.ArrayList;
import java.util.Arrays;
 
public class ArrayListToArray 
{
    public static void main(String[] args) 
    {
        ArrayList<String> al = new ArrayList<>();
         
        al.add("Tech");
        al.add("Blog");
        al.add("Station");
         
        //Converting the ArrayList to Object Array
        Object[] arr = al.toArray();
         
        System.out.println("The array - "+ Arrays.toString(arr) );
 
        //Iterating the array and converting the element to desired type
        for(Object object : arr) {
            String str = (String) object;
             
            System.out.println(str);
        }
    }
}

Output :

The array - [Tech, Blog, Station] 
Tech 
Blog 
Station 

Convert ArrayList to Array Java Example using – public <T> T[] toArray(T[] a)

This method is not bound to any specific type to be passed as an argument and specific type to be retuned.

The type of the argument of this method and the type of returned array is determined at Runtime.

Whichever type of Array is passed as parameter of this method, the same type of array will be returned. So the type of returned array will be same as the passed array.

Below points to be noted for this method –

  • If the array passed as an argument has enough space then the elements will be stored in this array itself.
  • However, If the array does not have enough space then a new array will be created. The new array will be of same type and size.
  • Suppose, If the array has additional space then firstly the array is filled with list elements and then with null values.
  • Suppose, If the runtime type of array is not a super type of the runtime type of every element in the list then the array throws an ArrayStoreException.

In the below Example , We are illustrating :

  • convert an ArrayList to String Array using the toArray(T[] a) method.
  • iterate through array content .
import java.util.ArrayList;
import java.util.Arrays;
 
public class ArrayListToArray
{
    public static void main(String[] args) 
    {
        ArrayList<String> al = new ArrayList<>();
         
        al.add("Tech");
        al.add("Blog");
        al.add("Station");
        al.add("Examples");
         
        //Converting the ArrayList to String Array
        //See we have passed a String Array to returned array will also be String
        String[] arr = al.toArray(new String[al.size()]);
         
        System.out.println(Arrays.toString(arr));
    }
}

Output :

 [Tech, Blog, Station, Examples] 

get() – to convert ArrayList to Array Java

We can use the get() method of ArrayList to convert the ArrayList to Array in Java.

 public E get(int index) 

This process will return the deep copy of ArrayList.

In the below Example , We are illustrating :

  • convert an ArrayList to String Array using the get() method.
  • deep copy of ArrayList.
  • alternative of using toArray() method.
import java.util.List; 
import java.util.ArrayList; 
  
public class ArrayListToArray 
{ 
    public static void main (String[] args) 
    { 
        List<String> list = new ArrayList<>(); 
        list.add("Tech"); 
        list.add("Blog"); 
        list.add("Station"); 

        String[] arr = new String[list.size()]; 
  
        // Converting ArrayList to Array manually using get() method 
        for (int i =0; i < list.size(); i++) 
        {
            arr[i] = list.get(i); 
        }
        
        //Iterating
        for (String str : arr) 
            System.out.print(str + " "); 
    } 
} 

Output:

 Tech Blog Station

Conclusion

We have learnt following ways to convert the ArrayList to Array in Java and with Examples :

  • Using toArray() method.
  • Using toArray(T[] a) method.
  • Using get() method.

Thanks for Reading!

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply