Java ArrayList.toArray() – Examples

In this tutorial, we will learn about the Java ArrayList.toArray() method, and learn how to use this method to convert this ArrayList to array, or set an array with the elements of this ArrayList, with the help of examples.

toArray()

ArrayList.toArray() returns an array containing all of the elements in this list in proper sequence (from first to last element).

Syntax

The syntax of toArray() method is

ArrayList.toArray()

Returns

The method returns Object[]

Example 1 – toArray()

In this example, we will convert ArrayList into an array with elements of type Object using ArrayList.toArray() method.

Java Program

import java.util.ArrayList;
import java.util.Arrays;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>(
				Arrays.asList("a", "b", "c", "d"));

		Object arr[] = arrayList.toArray();
		for (Object obj: arr) {
			System.out.println(obj);
		}
	}
}

Output

a
b
c
d

toArray(T[] a)

ArrayList.toArray() returns an array containing all of the elements in this ArrayList in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

toArray() sets the argument with the elements of this ArrayList, if it is big enough. Else, a new array of the same runtime type is allocated for this purpose and returned.

Syntax

The syntax of toArray() method with array of specific type T is

ArrayList.toArray(T[] arr)

where

ParameterDescription
arrThe array into which the elements of the list are to be stored.

Returns

The method returns <T> T[]

Example 2 – toArray(T[] a)

In this example, we will convert ArrayList into an array of type String using ArrayList.toArray(array) method. We shall pass a String Array of size equal to the size of this ArrayList.

The array we passed as argument will be set with the elements from the ArrayList, and also is returned by toArray() method.

Java Program

import java.util.ArrayList;
import java.util.Arrays;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>(
				Arrays.asList("a", "b", "c", "d"));

		String arr[] = new String[arrayList.size()];
		String arrReturned[] = arrayList.toArray(arr);
		
		System.out.println("Array passed as arguement");
		for (Object obj: arr) {
			System.out.println(obj);
		}
		
		System.out.println("\nArray returned by toArray()");
		for (Object obj: arrReturned) {
			System.out.println(obj);
		}
	}
}

Output

Array passed as arguement
a
b
c
d

Array returned by toArray()
a
b
c
d

Example 3 – toArray(T[] a)

The array we passed as argument to toArray() will be set with the elements from the ArrayList, and also is returned by toArray() method.

Since the array arr size is less than the size of ArrayList, a new array of String type is allocated with the size of ArrayList and returned.

Java Program

import java.util.ArrayList;
import java.util.Arrays;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>(
				Arrays.asList("a", "b", "c", "d"));

		String arr[] = new String[2];
		String arrReturned[] = arrayList.toArray(arr);
		
		System.out.println("Array passed as argument");
		for (Object obj: arr) {
			System.out.println(obj);
		}
		
		System.out.println("\nArray returned by toArray()");
		for (Object obj: arrReturned) {
			System.out.println(obj);
		}
	}
}

Output

Array passed as argument
null
null

Array returned by toArray()
a
b
c
d

Example 4 – toArray(T[] a)

The array we passed as argument to toArray() will be set with the elements from the ArrayList, and also is returned by toArray() method.

Since the array arr size is greater than the size of ArrayList, the argument will be set with the elements from the ArrayList, and also is returned by toArray() method.

Java Program

import java.util.ArrayList;
import java.util.Arrays;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>(
				Arrays.asList("a", "b", "c", "d"));

		String arr[] = new String[5];
		String arrReturned[] = arrayList.toArray(arr);
		
		System.out.println("Array passed as argument");
		for (Object obj: arr) {
			System.out.println(obj);
		}
		
		System.out.println("\nArray returned by toArray()");
		for (Object obj: arrReturned) {
			System.out.println(obj);
		}
	}
}

Output

Array passed as argument
a
b
c
d
null

Array returned by toArray()
a
b
c
d
null

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList.toArray() method, and also learnt how to use this method with the help of Java example programs.