Home Tutorial Java Collections Arraylist Java arraylist to array

 
 

Share on Google+Share on Google+
Java arraylist to array
Posted on: October 23, 2009 at 12:00 AM
Advertisement
This section demonstrates how to use Java ArrayList that can be converted into array

  • Java arrayList has method toArray()
  • It converts the given list into the array of Object.
  • It needs to be casted to the required data type.
  • Then it can be used properly.


Example of Java Arraylist to Array

import java.util.ArrayList;

public class List1 {
    public static void main(String[] args) {
     int ar[]={444,222,333,111};
     ArrayList list=new ArrayList();
     list.add(ar[0]);
     list.add(ar[1]);
     list.add(ar[2]);
     list.add(ar[3]);
     Object obj[]=list.toArray();
     for(Object  x:obj)
     {
         System.out.println((Integer)x-1);
     }
  }
}

Output

443
221
332
110
Advertisement

Related Tags for Java arraylist to array:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 23, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com