ArrayList toArray() method in Java with Examples Last Updated : 17 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.Declaring toArray() methodpublic Object[] toArray() or public <T> T[] toArray(T[] a)Parameters: This method either accepts no parameters or it takes an array T[] as a parameter which is the array into which the elements of the list are to be stored if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.Return Value: The function returns an array containing all the elements in this list.Exception: The first overload of this method throws no exceptions. However, the second overload throws the following exceptions:ArrayStoreException: if the runtime type of the specified array is not a supertype of the runtime type of every element in this list.NullPointerException if the specified array is nullExamples of Java ArrayList.toArray() methodExample 1 :Converting an ArrayList to an Array using toArray() Method in JavaThe following Java program demonstrates how to convert an ArrayList to an array using the toArray() method. This example showcases the basic usage of the ArrayList and how its elements can be retrieved and stored in the array. Java // Java Program to illustrate the // ArrayList toArray() method in Java import java.util.*; public class GFG { public static void main(String[] args) { // create object of ArrayList ArrayList<Integer> ArrLis = new ArrayList<Integer>(); // Add elements ArrLis.add(32); ArrLis.add(67); ArrLis.add(98); ArrLis.add(100); // print ArrayList System.out.println("ArrayList: " + ArrLis); // Get the array of the elements // of the ArrayList // using toArray() method Object[] arr = ArrLis.toArray(); System.out.println("Elements of ArrayList" + " as Array: " + Arrays.toString(arr)); } } OutputArrayList: [32, 67, 98, 100] Elements of ArrayList as Array: [32, 67, 98, 100] Explanation of the Program:This Java program demonstrates the use of ArrayList's toArray() method to convert its elements into a regular array. First, integers are added to the ArrayList. Then, the toArray() method is called to obtain an array representation of the ArrayList's elements, which is printed using Arrays.toString(arr).Example 2 : Converting an ArrayList to a Typed Array using toArray(T[]) Method in JavaThis Java program demonstrates how to convert an ArrayList of integers into an array of the integers using the toArray(T[]) method. This method is particularly useful when we want to specify the type of the array ensuring the type safety and avoiding the need for the type casting. Java // Java Program to illustrate the // ArrayList toArray(T[]) method in Java import java.util.*; // Driver Class public class GFG { // main function public static void main(String[] args) { // create object of ArrayList ArrayList<Integer> ArrLis = new ArrayList<Integer>(); // Add elements ArrLis.add(32); ArrLis.add(67); ArrLis.add(98); ArrLis.add(100); // print ArrayList System.out.println(" ArrayList: " + ArrLis); // Get the array of the elements // of the ArrayList // using toArray(T[]) method Integer arr[] = new Integer[ArrLis.size()]; arr = ArrLis.toArray(arr); System.out.println(" Elements of ArrayList " + "as Array: " + Arrays.toString(arr)); } } Output ArrayList: [32, 67, 98, 100] Elements of ArrayList as Array: [32, 67, 98, 100] Explanation of the Program:This Java program showcases the ArrayList method toArray(T[]), which converts an ArrayList to an array of specified type T. First, integers are added to ArrLis. Then, an Integer array of the same size is initialized and passed to toArray(arr) to obtain the array representation of ArrLis, printed using Arrays.toString(arr). Comment More infoAdvertise with us Next Article ArrayList toArray() method in Java with Examples P ProgrammerAnvesh Follow Improve Article Tags : Java Java-Collections Java - util package Java-Arrays Java-Functions Java-ArrayList +2 More Practice Tags : JavaJava-Collections Similar Reads AbstractSet toArray() method in Java with Example The toArray() method of Java AbstractSet is used to form an array of the same elements as that of the AbstractSet. Basically, it copies all the element from a AbstractSet to a new array. Syntax: Object[] arr = AbstractSet.toArray() Parameters: The method does not take any parameters. Return Value: T 2 min read ArrayList size() Method in Java with Examples In Java, the size() method is used to get the number of elements in an ArrayList.Example 1: Here, we will use the size() method to get the number of elements in an ArrayList of integers.Java// Java program to demonstrate the use of // size() method for Integer ArrayList import java.util.ArrayList; p 2 min read AbstractSet toArray(T[]) method in Java with Example The toArray(T[]) method method of AbstractSet class in Java is used to form an array of the same elements as that of the AbstractSet. It returns an array containing all of the elements in this AbstractSet in the correct order; the run-time type of the returned array is that of the specified array. I 4 min read Arrays asList() Method in Java with Examples The Arrays.asList() method in Java is part of the java.util.Arrays class, which is used to convert an array into a fixed-size list. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.toArray(). Example:Below is a simple example of the Arrays as 4 min read CharArrayWriter toCharArray() method in Java with examples The toCharArray() method of the CharArrayWriter class in Java returns a copy of the input data.Syntax: public char[] toCharArray() Parameters: This method does not accept any parameter.Return Value: This method returns a copy of the input data. Below program illustrate the above method: Program 1: J 2 min read CopyOnWriteArraySet toArray() method in Java with Example toArray() The Java.util.concurrent.CopyOnWriteArraySet.toArray() method returns an array containing all the elements in the set in proper sequence i.e. from first to last. The returned array will be safe as a new array is created (hence new memory is allocated). Thus the caller is free to modify the 3 min read ArrayList trimToSize() Method in Java with Example In Java, the trimToSize() method is used to reduce the capacity of an ArrayList to match its current size. This helps to optimize memory usage when an ArrayList contains less elements than its allocated capacity.Example 1: Here, we will use the trimToSize() method to trim an ArrayList of Strings.Jav 2 min read DelayQueue toArray() method in Java with Examples The toArray() method of DelayQueue is used to return an array containing all the elements in DelayQueue. There elements are not in any specific order in the array.Syntax: public Object[] toArray () or public T[] toArray (T[] a) Parameters: This method either accepts no parameters or it takes an arra 4 min read AbstractSequentialList toArray() method in Java with Example The toArray() method of Java AbstractSequentialList is used to form an array of the same elements as that of the AbstractSequentialList. Basically, it copies all the element from a AbstractSequentialList to a new array. Syntax: Object[] arr = AbstractSequentialList.toArray() Parameters: The method d 2 min read AbstractSequentialList toArray(T[]) method in Java with Example The toArray(arr[]) method of AbstractSequentialList class in Java is used to form an array of the same elements as that of the AbstractSequentialList. It returns an array containing all of the elements in this AbstractSequentialList in the correct order; the run-time type of the returned array is th 4 min read Like