Convert List to Array in Java

In this tutorial we will see how to convert a list of objects to an Array.  That can be done by toArray method on the list. We are going to use the below two methods to do the conversion. Both these methods preserve order of the original list being converted. We would also discuss the conversion of  a list to a primitive type array.

 Object[] toArray()

This method returns an array of Objects and the returned array has the same sequence as the list being converted.  The returned array is a new array and hence the modifications to the returned array will not modify the contents of the list.

 Example

package com.programtalk.beginner.tutorial;

package com.programtalk.beginner.tutorial;
import java.util.ArrayList;
import java.util.List;

public class ConvertList2ObjectArray {
	
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("one");
		list.add("two");
		list.add("three");
		list.add("four");
		
		System.out.println("Printing object array");
		Object[] objectArray = list.toArray();
		printAnArrayObjects(objectArray);
	}

	public static void printAnArrayObjects(Object[] strArray){
		for(Object each:strArray){
			System.out.println(each);
		}
	}
}

Output

one
two
three
four

<T> T[] toArray(T[] a)

Type of Output array

  toArray(T[] a) method allows precise control over the runtime type of the output array. So here you can specify the array type that you want to retrieve after the conversion. In the below example, you can see that array returned is a String[] rather than the Object[] array returned by the Object[] toArray() method. We would look at the argument new String[0] passed to toArray in the next section.
Example
package com.programtak.beginner.tutorial;

import java.util.ArrayList;
import java.util.List;

public class ConvertList2StringArray {
	
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("one");
		list.add("two");
		list.add("three");
		list.add("four");
		
		System.out.println("Printing String array");
		String[] stringArray = list.toArray(new String[0]);
		printStringArray(stringArray);
	}

	public static void printStringArray(String[] strArray){
		for(String each:strArray){
			System.out.println(each);
		}
	}
}

Output

one
two
three
four

Understanding the argument passed

Pass in new ArrayType[0]

In the above example we passed new String[0] to the toArray() method. This meant that we are passing a zero sized array.  The following code dumps the list into a newly allocated array of String.


String[] stringArray = list.toArray(new String[0]);

Pass in array having length more than the list
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
In the below example we will pass in an array with capacity more than the size of the list. And you would see in the output that the rest of the elements have been set to null.
package com.programtak.beginner.tutorial;

import java.util.ArrayList;
import java.util.List;

public class ConvertList2StringArray {
	
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("one");
		list.add("two");
		list.add("three");
		list.add("four");
		
		System.out.println("Printing String array");
		String[] stringArray = list.toArray(new String[6]);
		printStringArray(stringArray);
	}

	public static void printStringArray(String[] strArray){
		for(String each:strArray){
			System.out.println(each);
		}
	}
}

And the output is:

one
two
three
four
null
null

Convert to a primitive type array

The list canot be converted to a primitive type array using the above described methods of list. For this conversion, the list has to be iterated and the primitive type array has to be filled as shown in the below example

 


package com.programtalk.beginner.tutorial;
import java.util.ArrayList;
import java.util.List;

public class ConvertList2PrimitiveIntArray {
	
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<>();
		list.add(1); 
		list.add(2);
		list.add(2);
		list.add(4);
		
		System.out.println("converting to primitive int array");
		int[] intArray = new int[list.size()];		
		for(int i = 0; i < list.size(); i++){
			intArray[i] = list.get(i);
		}
		System.out.println("convertion done");
		
		
		System.out.println("print array");
		printIntArray(intArray);
		
	}

	public static void printIntArray(int[] intArray){
		for(int each:intArray){
			System.out.println(each);
		}
	}
	
}

output:


converting to primitive int array
convertion done
print array
1
2
2
4

 

Other ways of conversion

Java 8 streams

Using the java 8 stream api, the conversion can be done as below:


List<String> stringList = Arrays.asList("one","two");
String[] stringArray = stringList.stream().toArray(String[]::new);

 

Arrays.copyOf

The conversion to a specific type array can also be achieved by using the Arrays.copyOf. This is not a recommended approach as it will be done in three steps but is good to know.


List<String> stringList = Arrays.asList("one","two");
Object[] objectList = stringList.toArray();
String[] stringArray = Arrays.copyOf(objectList,objectList.length,String[].class);

Usage

Collections.sort

This javadoc explains why the list is dumped into a array before sorting

This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place

Here is the implementation from java 7.


 public static <T extends Comparable<? super T>> void sort(List<T> list) {
   Object[] a = list.toArray();
    Arrays.sort(a);
    ListIterator<T> i = list.listIterator();
    for (int j=0; j<a.length; j++) {
       i.next();
       i.set((T)a[j]);
    }
}

1 thought on “Convert List to Array in Java”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.