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
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.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
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]); } }
Java 8 streams are really helpful and make the conversion very easy and concise. Thanks