Sunday, 25 May 2014

LinkedList : toArray (T[] a) : Copy List data to Array Of Specific type

public <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence, the run time type of the returned array is that of the specified array.

import java.util.*;

class LinkedListSpecificArray{
 public static void main(String args[]){
  LinkedList<Integer> myList;
  myList = new LinkedList<> ();
  
  Integer myArray[];
  Integer retArray[];
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(123);
  myList.add(321);
  myList.add(456);
  
  int size = myList.size();
  
  myArray = new Integer[size];
  retArray = new Integer[size];
  
  retArray = myList.toArray(myArray);
  
  System.out.println("Elements in the list are");
  System.out.println(myList);
  
  System.out.println("Elements in myArray are");
  for(int i: myArray)
   System.out.print(i +" ");
   
  System.out.println("\nElements in the retArray are");
  for(int i: retArray)
   System.out.print(i +" "); 
 }
}

Output
Elements in the list are
[10, 123, 321, 456]
Elements in myArray are
10 123 321 456
Elements in the retArray are
10 123 321 456

1. If the Array size is more than the number of elements in the list, then the element in the array immediately following the end of the list is set to null.

import java.util.*;
class LinkedListSpecificArray1{
 public static void main(String args[]){
  LinkedList<Integer> myList;
  myList = new LinkedList<> ();
  
  Integer myArray[];
  Integer retArray[];
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(123);
  myList.add(321);
  myList.add(456);
  
  int size = myList.size();
  
  myArray = new Integer[2*size];
  retArray = new Integer[2*size];
  
  retArray = myList.toArray(myArray);
  
  System.out.println("Elements in the list are");
  System.out.println(myList);
  
  System.out.println("Elements in myArray are");
  for(Integer i: myArray)
   System.out.print(i +" ");
   
  System.out.println("\nElements in the retArray are");
  for(Integer i: retArray)
   System.out.print(i +" "); 
 }
}

Output
Elements in the list are
[10, 123, 321, 456]
Elements in myArray are
10 123 321 456 null null null null
Elements in the retArray are
10 123 321 456 null null null null

2. toArray() throws ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
import java.util.*;
class LinkedListSpecificArrayStore{
 public static void main(String args[]){
  LinkedList<Integer> myList;
  myList = new LinkedList<> ();
  
  Float myArray[];
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(123);
  myList.add(321);
  myList.add(456);
  
  int size = myList.size();
  
  myArray = new Float[size];
  
  myList.toArray(myArray);
  
  System.out.println("Elements in myArray are");
  for(float i: myArray)
   System.out.print(i +" ");
 }
}

Output
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
        at java.util.LinkedList.toArray(LinkedList.java:1090)
        at LinkedListSpecificArrayStore.main(LinkedListSpecificArrayStore.java:19)

When you tries to run the above program, run time throws "ArrayStoreException". Since the list is of type Integer, where as the destination is of type Float. Both are incompatible.

3. toArray() throws NullPointerException if the specified array is null 

import java.util.*;
class LinkedListSpecificArrayNull{
 public static void main(String args[]){
  LinkedList<Integer> myList;
  myList = new LinkedList<> ();
  
  Integer myArray[];
  myArray = null;
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(123);
  myList.add(321);
  myList.add(456);
  
  int size = myList.size();
  
  myList.toArray(myArray);
  
  System.out.println("Elements in myArray are");
  for(Integer i: myArray)
   System.out.print(i +" ");
 }
}

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.LinkedList.toArray(LinkedList.java:1084)
        at LinkedListSpecificArrayNull.main(LinkedListSpecificArrayNull.java:18)

Since “myArray” is initialized to null, so at run time NullPointerException thrown.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment