Sunday 1 June 2014

ArrayList : toArray (T[] a) : Copy List Data to Array of Specific Type

<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 ArrayListSpecificArray{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  
  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("\n Elements 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 ArrayListSpecificArray{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  
  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("\n Elements 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 ArrayListSpecificArrayStore{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  
  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 +" ");
 }
}

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.

Output
Exception in thread "main" java.lang.ArrayStoreException
        at java.lang.System.arraycopy(Native Method)
        at java.util.ArrayList.toArray(ArrayList.java:393)
        at ArrayListSpecificArrayStore.main(ArrayListSpecificArrayStore.java:18)

3. toArray() throws NullPointerException if the specified array is null
import java.util.*;
class ArrayListSpecificArrayNull{
 public static void main(String args[]){
  ArrayList<Integer> myList = new ArrayList<> ();
  
  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 +" ");
 }
}

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

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.ArrayList.toArray(ArrayList.java:390)
        at ArrayListSpecificArrayNull.main(ArrayListSpecificArrayNull.java:17)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment