toArray() in Java

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.
The above image shows the conversion of an ArrayList to an array.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
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[].
Turn Learning into Career Growth
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.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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[]).