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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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[]).
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more