Sunday, 25 May 2014

LinkedList : toArray : Get the Array Representation Of List

public Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence.

import java.util.*;

class LinkedListToArray{
 public static void main(String args[]){
  LinkedList<Integer> myList;
  Object myArray[];
  
  myList = new LinkedList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  myList.add(40);
  
  int size = myList.size();
  myArray = new Object[size];
  
  System.out.println("Copying myList data to myArray");
  
  myArray = myList.toArray();
 
  System.out.println("Elements in myArray are");
  for(Object obj : myArray)
   System.out.println(obj);
 }
}

Output
Copying myList data to myArray
Elements in myArray are
10
20
30
40



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment