Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java ArrayList to Array

This small Java example shows how to convert ArrayList to array using toArray method of Java ArrayList class.
  1. import java.util.ArrayList;
  2.  
  3. //Java ArrayList to Array using toArray method
  4. public class JavaArrayListToArray {
  5.  
  6.     public static void main(String args[]){
  7.        
  8.         //create new ArrayList
  9.         ArrayList<String> aListColors = new ArrayList<String>();
  10.         aListColors.add("Red");
  11.         aListColors.add("Green");
  12.         aListColors.add("Blue");
  13.        
  14.         //Java ArrayList to Array using toArray method.
  15.        
  16.         /*
  17.          * To convert Java ArrayList to Array, use
  18.          * Object[] toArray()
  19.          * method of ArrayList class.
  20.          */
  21.        
  22.         Object[] colors = aListColors.toArray();
  23.        
  24.         //Java ArrayList converted to Array
  25.         for(int i=0; i< colors.length; i++)
  26.             System.out.println(colors[i]);
  27.     }
  28. }
No votes yet
Share this

Post new comment

3 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.