Object[] toArray()The method has no argument and the return is an Object Array. This method creates a new array that contains all the elements of the List. Here is a simple example:
List<String> myList = new ArrayList<String>(); myList.add("Apple"); myList.add("Banana"); myList.add("Orange"); Object[] myArray = myList.toArray(); for (Object myObject : myArray) { System.out.println(myObject); }As shown, the toArray() returns an array of objects. The array myArray will have 3 items - which is the same as the source List. Hence, below is the expected output:
Apple Banana OrangeNote that the return type is strictly object array. If we try to cast the result to another type of array (E.g. String array), there will be an exception thrown. Here is an example:
List<String> myList = new ArrayList<String>(); myList.add("Apple"); myList.add("Banana"); myList.add("Orange"); String[] myArray = (String[]) myList.toArray();
And below is the result when the code above is run. A ClassCastException is thrown at runtime.
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; at Test.main(Test.java:10)Note also that the toArray() should return a "safe" array - meaning the List will have no reference on the elements of the returned array. In other words, if you modify the array, the List is not affected. Consider the example below:
List<String> myList = new ArrayList<String>(); myList.add("Apple"); myList.add("Banana"); myList.add("Orange"); Object[] myArray = myList.toArray(); myArray[0] = "X"; myArray[1] = "Y"; myArray[2] = "Z"; for (String myString:myList) { System.out.println(myString); }The modification on the resulting array (myArray) will not affect the original List (myList). Hence, the output of the code will display the original contents of the List, as shown below:
Apple Banana Orange
If we want the result of the toArray method to return a specific type of array (E.g. String array), we can pass what type of array it should return. Here is the syntax:
<T> T[] toArray(T[] a)The JavaDoc says that the parameter is "the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.". Here is an example code:
List<String> myList = new ArrayList<String>(); myList.add("Apple"); myList.add("Banana"); myList.add("Orange"); String[] myArray = myList.toArray(new String[0]); for (String myString : myArray) { System.out.println(myString); }
The code passes a String array with length 0. Since the elements will not fit, a new Array is created and returned to the caller.
Alternately, we can pass an array that is big enough to contain the elements of the List. Here is an example:
List<String> myList = new ArrayList<String>(); myList.add("Apple"); myList.add("Banana"); myList.add("Orange"); String[] myArray = new String[3]; myList.toArray(myArray); for (String myString:myArray) { System.out.println(myString); }Notice that we don't even need to assign the return of the toArray method to a variable. The contents are stored in the array parameter to the method. The array myArray in this case will get the values of the List. And if we run it, the following will be displayed:
Apple Banana OrangeOne pitfall to consider is that we can't convert from a List of a certain type to an array of another type automatically using the toArray method. Here is an example code:
List<String> myList = new ArrayList<String>(); myList.add("100"); myList.add("200"); myList.add("300"); Integer[] myArray = myList.toArray(new Integer[0]);
The code above will throw an ArrayStoreException. Because it can not convert the List of String to an array of Integer.
Exception in thread "main" java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at java.util.Arrays.copyOf(Arrays.java:2775) at java.util.ArrayList.toArray(ArrayList.java:327) at Test.main(Test.java:10)
List<String> myList = new ArrayList<String>(); myList.add("Apple"); myList.add("Banana"); myList.add("Orange"); String[] myArray = new String[myList.size()]; for (int i = 0; i < myList.size(); i++) { myArray[i] = myList.get(i); } for (String myString : myArray) { System.out.println(myString); }As usual, below is the expected output:
Apple Banana OrangeOne benefit of using custom code is that we can have additional logic not included in ready made helper classes. For example, if we wish to transform the original elements of a List to be the items of the resulting array. Here is a simple example:
List<String> myList = new ArrayList<String>(); myList.add("Apple"); myList.add("Banana"); myList.add("Orange"); String[] myArray = new String[myList.size()]; for (int i = 0; i < myList.size(); i++) { myArray[i] = "This is " + myList.get(i); } for (String myString : myArray) { System.out.println(myString); }As shown, the original List contains names of fruits. The resulting array contains phrases that uses the fruit. Below is the output of the code:
This is Apple This is Banana This is Orange
List<String> myList = new ArrayList<String>(); myList.add("100"); myList.add("200"); myList.add("300"); Integer[] myArray = new Integer[myList.size()]; for (int i = 0; i < myList.size(); i++) { myArray[i] = Integer.valueOf(myList.get(i)); } for (Integer myInt : myArray) { System.out.println(myInt); }
The output is shown below:
100 200 300
Similarly, here is an example of custom code that converts List of String to an Array of Double values.
List<String> myList = new ArrayList<String>(); myList.add("100.50"); myList.add("200.75"); myList.add("300.98"); Double[] myArray = new Double[myList.size()]; for (int i = 0; i < myList.size(); i++) { myArray[i] = Double.valueOf(myList.get(i)); } for (Double myDouble : myArray) { System.out.println(myDouble); }
The output is displayed below:
100.5 200.75 300.98
Java 8's stream offers very cool ways of transforming objects. Below is an example of how to convert a List to Array in Java 8 using streams:
List<String> myList = new ArrayList<String>(); myList.add("Apple"); myList.add("Banana"); myList.add("Orange"); String[] myArray = myList.stream().toArray(String[]::new);