toArray() in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Overview

The toArray() method of the ArrayList class is used to convert the ArrayList to an array and return the new array.

Arrays are static i.e their size is fixed and we cannot change it once created but ArrayList instances are dynamic i.e their size can be changed.

Example of toArray() in Java

Output-

Explanation:

  • We have created an ArrayList and added elements to it using the add() method.
  • Then, we have converted the ArrayList instance to an array using the toArray() method.

example of toarray in java The above image shows the conversion of an ArrayList to an array.

Syntax of toArray() in Java

The below syntax is for converting an ArrayList to an array that returns an Object array.

The below syntax is written in generics terms for converting an ArrayList of type T to an array that returns an Object array that is of type T.

The above syntax contains a parameter which is a an array of Type T used to store the elements of the list to be converted. This method converts the list to an array which is of type T and returns it.

toArray() Parameters

This function either takes in no parameter as per the first syntax or takes in an array of Type T in which the element of the list will be stored.

toArray() Return Values

The toArray() method returns an Object array if no parameter is passed. Otherwise, if an array is passed as an argument, it returns a new array of Type T i.e. T[].

Exceptions of toArray() in Java

  • It throws the NullPointerException if the specified array is null. Example-

Output-

  • It throws ArrayStoreException at runtime when an attempt is made to store the incorrect type of object in an array of objects. Example-

Output-

More about toArray() in Java

  • The toArray() method is used to convert an ArrayList to an array.
  • When no parameter is passed, it returns an array of Object instances. It is so because arrays were a part of Java since the beginning and Generics got introduced later.
  • The type information of ArrayList is not available due to type erasure during runtime.
  • So in simple words, our Java program does not know what type of array it should create to copy data to. The function toArray() was hence set to return Object[].

Example 1: Using a Parameter

In our program, we have created an ArrayList of Integers and then converted it to an array using the toArray() method.

Output-

Example 2: Without Parameter

Here we have directly converted the ArrayList to an array without the parameter. Hence, an array of objects gets returned.

Output-

Explanation:

  • We have created an ArrayList of String and added elements to it. Then we have sorted the elements alphabetically.
  • Then we have converted the ArrayList to array without passing any parameter and printed the elements.

Conclusion-

  • The toArray() method of the ArrayList is used to convert an ArrayList to an array in Java.
  • This function either takes in no parameter or takes in an array of Type T(T[] a) in which the element of the list will be stored.
  • The toArray() function returns an Object array if no argument is passed. Otherwise, if the parameter is passed, it returns an array of Type T(T[]).