12:43 pm - Tuesday September 25, 2018

ArrayList Example Program for toArray(T[] a)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044
/**
@AUTHOR:candidjava.com
@DESCRIPTION:returning the array elements in the proper sequence by copying the elements of the arraylist to the object element.
**/
import java.util.*;
public class ToArray
{
  public static void main(String args[])
  {
     ArrayList<String> al=new ArrayList<String>();//creating the Arraylist object
     al.add("sam");
     al.add("abi");
     al.add("guru");
     System.out.println("Array list elements:"+al);
     Object ob[]={"ram","geeth"}; //creating the object variable
     System.out.println("Elements of object ");
     for (int a = 0; a < ob.length; a++) //displaying the object elements
      {
         System.out.println(ob[a].toString());
      }
     ob=al.toArray(ob); //existing elements of object is replaced with the Arraylist elements
     System.out.println("elements of object after toArray:");
     for(int i=0;i<ob.length;i++) //displaying the object elements
      {
         System.out.println(ob[i].toString());
      }
  }
}

OUTPUT:
Array list elements:[sam, abi, guru]
Elements of object
ram
geeth
elements of object after toArray:
sam
abi
guru

Filed in: Z-A All Java Codes

Comments are closed.