Home Java ArrayList Methods toArray() Java ArrayList toArray() Method Definition In Java, toArray() copies the elements of an ArrayList into a new Object[], in list order. toArray(T[] a) copies them into an array of a specific type, such as String[], so you do not need casts afterwards.
The array is a copy : changing an array slot does not change the list, and the other way round. Arrays print as a code like [Ljava.lang.String;@..., so use Arrays.toString(arr) to see the contents.
An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe .
Syntax The typed form is the one you usually want.
List<String> list = new ArrayList<>(Arrays.asList("a", "b"));
Object[] objs = list.toArray(); // Object[]
String[] arr = list.toArray(new String[0]); // String[]
String[] arr2 = list.toArray(String[]::new); // Java 11+ Call What it does list.toArray()A new Object[] with all elements. list.toArray(new String[0])A new String[] of the right size. list.toArray(String[]::new)The same, with the Java 11 generator overload. Arrays.toString(arr)Prints the array contents.
Passing new String[0] is the common idiom: since the empty array is too small, toArray creates a new array of the correct type and size.
Library declarations // java.util.ArrayList<E>
public Object[] toArray()
public <T> T[] toArray(T[] a)
// java.util.Collection<E> (default method, Java 11+)
default <T> T[] toArray(IntFunction<T[]> generator) Parameters Parameter Description a (T[])Only in the typed form. Used directly if it is big enough; otherwise only its type is used to create a new array.
Return Value Return Value Description Object[] or T[]An array holding all elements in list order.
Important
toArray(a) throws NullPointerException if a is null, and ArrayStoreException if an element does not fit the array type. If a is larger than the list, the slot right after the last element is set to null and later slots are left alone. The result of toArray() is always Object[]; casting it to String[] throws ClassCastException.
Compare with Similar Methods Copying data out of a list:
Method How it differs clone()Copies into another ArrayList. subList(a, b)A view of a range, still a list. Arrays.asList(arr)The opposite direction: a fixed-size list backed by an array. forEach(action)Processes elements without copying them.
Java Examples 1. List to array 2. Object[] vs T[] 3. Array size rules Example 1 — Convert a List to a String Array Copy the fruits into a String[], then change the array.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListToArrayDemo {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>(Arrays.asList("kiwi", "mango", "plum"));
String[] arr = fruits.toArray(new String[0]);
System.out.println(arr.length + " " + Arrays.toString(arr));
arr[0] = "lime";
System.out.println("array: " + Arrays.toString(arr));
System.out.println("list: " + fruits);
}
} 3 [kiwi, mango, plum]
array: [lime, mango, plum]
list: [kiwi, mango, plum] How It Works Right type. The empty new String[0] tells toArray to build a String[], sized for all three fruits.Readable print. Arrays.toString shows the array contents in brackets, just like a list prints.Edit the copy. arr[0] = "lime" changes only the array slot.List untouched. The list still starts with kiwi, because the array is an independent copy.Example 2 — Object[] vs a Typed Array Compare the no-argument form with the typed form when summing numbers.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListToArrayTypes {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>(Arrays.asList(4, 5, 6));
Object[] objs = nums.toArray();
System.out.println("Object[] element type: " + objs.getClass().getComponentType().getSimpleName());
Integer[] boxed = nums.toArray(new Integer[0]);
int sum = 0;
for (int n : boxed) {
sum += n;
}
System.out.println("Integer[] sum: " + sum);
int second = (Integer) objs[1];
System.out.println("second via Object[]: " + second);
}
} Object[] element type: Object
Integer[] sum: 15
second via Object[]: 5 How It Works Plain form. toArray() always returns an Object[]; its component type is Object, not Integer.Typed form. toArray(new Integer[0]) returns an Integer[], so the loop can unbox each element straight into an int.Sum. 4 + 5 + 6 = 15.Casting each element. Reading from the Object[] needs a cast per element, (Integer) objs[1], which is why the typed form is usually nicer.Example 3 — What Happens with an Exact or Larger Array Pass an array that fits exactly, then one that is too big.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListToArraySized {
public static void main(String[] args) {
List<String> codes = new ArrayList<>(Arrays.asList("A1", "B2"));
String[] exact = new String[2];
String[] result = codes.toArray(exact);
System.out.println("same array returned? " + (result == exact));
String[] big = {"x", "x", "x", "x"};
codes.toArray(big);
System.out.println("bigger array: " + Arrays.toString(big));
}
} same array returned? true
bigger array: [A1, B2, null, x] How It Works Exact fit. The two-slot array is big enough, so toArray fills it and returns that same array instead of making a new one.Too big. The four-slot array is also filled in place, starting at index 0.null marker. The slot right after the last element (index 2) is set to null, marking where the list's data ends.Rest untouched. Index 3 still holds the old "x". Passing new String[0] avoids these leftovers.Frequently Asked Questions How do I convert an ArrayList to an array in Java? Use list.toArray(new String[0]) (with your element type), or list.toArray(String[]::new) on Java 11 and later.
Why does toArray() return Object[] instead of String[]? Because of type erasure the list does not know its element type at run time. Pass a typed array to get a String[].
Should I pass new String[0] or new String[list.size()]? Both work. new String[0] is the common, simplest choice and is usually just as fast.
Does changing the array change the ArrayList? No. toArray copies the elements, so the array and the list are independent.
How do I print an array returned by toArray()? Use Arrays.toString(array). Printing the array directly shows a type code and a hash, not the elements.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion