Saturday 10 May 2014

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

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

import java.util.*;
class VectorSpecificArray{
 public static void main(String args[]){
  Vector<Integer> myList = new Vector<> ();
  
  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 Vector 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 Vector 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 Vector, then the elements in the array immediately following the end of the Vector is set to null.
import java.util.*;
class VectorSpecificArray1{
 public static void main(String args[]){
  Vector<Integer> myList = new Vector<> ();
  
  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 Vector 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 Vector 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 VectorSpecificArrayStore{
 public static void main(String args[]){
  Vector<Integer> myList = new Vector<> ();
  
  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
        at java.lang.System.arraycopy(Native Method)
        at java.util.Vector.toArray(Vector.java:718)
        at VectorSpecificArrayStore.main(VectorSpecificArrayStore.java:18)

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 VectorSpecificArrayNull{
 public static void main(String args[]){
  Vector<Integer> myList = new Vector<> ();
  
  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.Vector.toArray(Vector.java:715)
        at VectorSpecificArrayNull.main(VectorSpecificArrayNull.java:17)





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment