Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*
- * Convert ArrayList to Array. Write a program to convert
- * ArrayList to Array. This program shows how to convert
- * ArrayList to Array.
- */
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class ConvertArrayListToArray {
- public static void main(String args[]){
- //create new ArrayList object
- ArrayList listColor = new ArrayList();
- listColor.add("Red");
- listColor.add("Blue");
- listColor.add("Green");
- /*
- * To convert ArrayList to array, use
- * toArray() method of ArrayList.
- *
- * This method return array of Objects.
- */
- Object[] colors = listColor.toArray();
- System.out.println("ArrayList converted to Array");
- for(int i=0; i < colors.length; i++)
- System.out.println(colors[i]);
- }
- }
- /*
- Output would be
- ArrayList converted to Array
- Red
- Blue
- Green
- */

Post new comment