How do I convert a list to an array?
Author: Deron Eriksson
Description: This Java example shows how to convert a list to an array.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)
Converted a list to an array can be accomplished by calling the toArray() method of the list with a parameter indicating the type of array to create. The ListToArrayTest class takes a list of strings and gets an array of strings from this list. ListToArrayTest.javapackage test; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.ArrayUtils; public class ListToArrayTest { public static void main(String[] args) throws IOException { List<String> strList = new ArrayList<String>(); strList.add("string1"); strList.add("string2"); strList.add("string3"); String[] strArray = (String[]) strList.toArray(new String[0]); System.out.println(ArrayUtils.toString(strArray)); } } Here is the output from ListToArrayTest. Results{string1,string2,string3} Related Tutorials: |