Tuesday 25 March 2014

toArray() : get the List Data as Object Array

Object[] toArray()
    Returns an array containing all of the elements in this list in proper sequence. The returned array is totally independent, Updating the Array won't affect List.


import java.util.*;
class ListToObjectArray{
 public static void main(String args[]){
  List<Float> myList = new ArrayList<> ();
  
  /* Add Elements to myList */
  myList.add(10.01f);
  myList.add(123.01f);
  myList.add(321.01f);
  myList.add(456.78f);
  
  Object myArr = myList.toArray();
  
  System.out.println("Elements in the Array are");
  for(Float f1 : myList)
   System.out.println(f1);
 }
}

Output
Elements in the Array are
10.01
123.01
321.01
456.78

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment