How to convert List to Array in Java?
UPDATED: 20 November 2014
Tags:
Array
,
Collection
,
List
Using List over regular Array is the best practice in Java. However in certain situations we have to convert List to Array. There are two versions of toArray() is available. Lets understand one after another.
When we need to convert Array to List?
- Method is not overloaded to support collection. (i.e Available only MyMethod(String[] str) not MyMethod(List
listStr)) - Passing an Array to a method in other API or framework.
toArray()
This will return an Array of Object[].
T[] toArray(T[] a)
This will return an Array of given data type T[]. This is much more convenient to use. This conversion has different scenarios while converting to Array.
- Same size of Array: When you have Array of same size of List, It will convert normally.
- Less size of Array: When you have Array has less size of List, It'll increase the size of Array.
- Greater size of Array: When you have Array with greater size of List, The element in the array immediately following the end of the list is set to null. (JAVADOC: This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
import java.util.ArrayList; import java.util.List; /** * @author javaQuery */ public class ListToArrayExample { public static void main(String[] args) { /* Create list of String */ List<String> listString = new ArrayList<String>(); /* Add element from 0th position */ listString.add("a"); listString.add("b"); listString.add("c"); /* Get an Array of Object */ Object[] obj = listString.toArray(); /* Loop through all elements */ for (Object object : obj) { /* Check object is instanceof String */ if (object instanceof String) { System.out.println(object.toString()); } } System.out.println("---------------------------------"); System.out.println("An array of same size of List"); /* Create an array of String */ String[] arrayString = new String[listString.size()]; /* Get Array of String */ arrayString = listString.toArray(arrayString); /* Loop through all elements */ for (String str : arrayString) { System.out.println(str); } System.out.println("---------------------------------"); System.out.println("An array of less size than List(*T[] toArray(T[] a) will increase the size of Array)"); /* Create an array of String */ String[] arrayStringLessSize = new String[2]; /* Get Array of String */ arrayStringLessSize = listString.toArray(arrayStringLessSize); /* Loop through all elements */ for (String str : arrayStringLessSize) { System.out.println(str); } System.out.println("---------------------------------"); System.out.println("An array of greater size than List"); System.out.println("(T[] toArray(T[] a): The element in the array immediately following the end of the list is set to null)"); /* Create an array of String */ String[] arrayStringGreaterSize = new String[5]; arrayStringGreaterSize[3] = "x"; arrayStringGreaterSize[4] = "y"; System.out.println("** Array before T[] toArray(T[] a) **"); /* Loop through all elements */ for (String str : arrayStringGreaterSize) { System.out.println(str); } /* Get Array of String */ arrayStringGreaterSize = listString.toArray(arrayStringGreaterSize); System.out.println("** Array after T[] toArray(T[] a) **"); /* Loop through all elements */ for (String str : arrayStringGreaterSize) { System.out.println(str); } } }
Output
a b c --------------------------------- An array of same size of List a b c --------------------------------- An array of less size than List(*T[] toArray(T[] a) will increase the size of Array) a b c --------------------------------- An array of greater size than List (T[] toArray(T[] a): The element in the array immediately following the end of the list is set to null) ** Array before T[] toArray(T[] a) ** null null null x y ** Array after T[] toArray(T[] a) ** a b c null y
Tags:
Array
,
Collection
,
List
0 comments :