Wednesday 19 March 2014

Copy Set Data to Array of Specific Type

<T> T[] toArray(T[] a)
    Returns an array containing all of the elements in this set; the run time type of the returned array is that of the specified array.

import java.util.*;
class SetCopyToSpecificArray{
 public static void main(String args[]){
  Set<Double> mySet = new TreeSet<> ();
  Double myArray[];
  int size;
  
  /* Add Elements to the Set */
  mySet.add(55.6);
  mySet.add(10.01);
  mySet.add(5.01);
  mySet.add(-100.123);
  
  /* Get the size of the Set*/
  size = mySet.size();
  
  /* Initialize the size of myArray */
  myArray = new Double[size];
  
  /* Copy Elements to the myArray of type Double*/
  mySet.toArray(myArray);
  
  /* Display the elements of the Array */
  System.out.println("Elements in the Array are");
  for(Double obj : myArray)
   System.out.println(obj);
 }
}

Output

Elements in the Array are
-100.123
5.01
10.01
55.6


If the set fits in the specified array, it is returned, Otherwise, a new array is allocated with the run time type of the specified array and the size of this set.

import java.util.*;
class SetCopyToSpecificArray{
 public static void main(String args[]){
  Set<Double> mySet = new TreeSet<> ();
  Double myArrayParam[] = new Double[2];
  Double myArrayReturn[];
  int size;
  
  /* Add Elements to the Set */
  mySet.add(55.6);
  mySet.add(10.01);
  mySet.add(5.01);
  mySet.add(-100.123);
  
  /* Copy Elements to the myArray of type Double*/
  myArrayReturn = mySet.toArray(myArrayParam);
  
  /* Display the elements of the Array */
  System.out.println("Elements in the myArrayParam are");
  for(Double obj : myArrayParam)
   System.out.println(obj);
   
  System.out.println("\nElements in the myArrayReturn are");
  for(Double obj : myArrayReturn)
   System.out.println(obj);
 }
}

Output

Elements in the myArrayParam are
null
null

Elements in the myArrayReturn are
-100.123
5.01
10.01
55.6
As you observe the output, the size of the Array myArrayParam is 2, which is less that the set size 4. So the elements in the set are not copied to the Array myArrayParam.

If the myArrayParam has the size 4, exactly as the set size, then the elements are copied to myArrayParam and at the same time to the Array myArrayReturn also.

import java.util.*;
class SetCopyToSpecificArray{
 public static void main(String args[]){
  Set<Double> mySet = new TreeSet<> ();
  Double myArrayParam[] = new Double[4];
  Double myArrayReturn[];
  int size;
  
  /* Add Elements to the Set */
  mySet.add(55.6);
  mySet.add(10.01);
  mySet.add(5.01);
  mySet.add(-100.123);
  
  /* Copy Elements to the myArray of type Double*/
  myArrayReturn = mySet.toArray(myArrayParam);
  
  /* Display the elements of the Array */
  System.out.println("Elements in the myArrayParam are");
  for(Double obj : myArrayParam)
   System.out.println(obj);
   
  System.out.println("\nElements in the myArrayReturn are");
  for(Double obj : myArrayReturn)
   System.out.println(obj);
 }
}

Output

Elements in the myArrayParam are
-100.123
5.01
10.01
55.6

Elements in the myArrayReturn are
-100.123
5.01
10.01
55.6


If the Array size is greater than the set size, then the element in the array immediately following the end of the set is set to null.

import java.util.*;
class SetCopyToSpecificArray{
 public static void main(String args[]){
  Set<Double> mySet = new TreeSet<> ();
  Double myArrayParam[] = new Double[10];
  Double myArrayReturn[] = new Double[10];
  int size;
  
  /* Add Elements to the Set */
  mySet.add(55.6);
  mySet.add(10.01);
  mySet.add(5.01);
  mySet.add(-100.123);
  
  /* Copy Elements to the myArray of type Double*/
  myArrayReturn = mySet.toArray(myArrayParam);
  
  /* Display the elements of the Array */
  System.out.println("Elements in the myArrayParam are");
  for(Double obj : myArrayParam)
   System.out.println(obj);
   
  System.out.println("\nElements in the myArrayReturn are");
  for(Double obj : myArrayReturn)
   System.out.println(obj);
 }
}
  
Output

Elements in the myArrayParam are
-100.123
5.01
10.01
55.6
null
null
null
null
null
null

Elements in the myArrayReturn are
-100.123
5.01
10.01
55.6
null
null
null
null
null
null


toArray method throws NullPointerException if the specified Array is null

import java.util.*;
class SetCopyToSpecificArray{
 public static void main(String args[]){
  Set<Double> mySet = new TreeSet<> ();
  Double myArrayParam[];
  myArrayParam = null;
  
  /* Add Elements to the Set */
  mySet.add(55.6);
  mySet.add(10.01);
  mySet.add(5.01);
  mySet.add(-100.123);
  
  /* Copy Elements to the myArray of type Double*/
  mySet.toArray(myArrayParam);
  
  /* Display the elements of the Array */
  System.out.println("Elements in the myArrayParam are");
  for(Double obj : myArrayParam)
   System.out.println(obj);
 }
}

Output

Exception in thread "main" java.lang.NullPointerException
        at java.util.AbstractCollection.toArray(Unknown Source)
        at SetCopyToSpecificArray.main(SetCopyToSpecificArray.java:15)


toArray throws ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type of every element in this set.

import java.util.*;
class SetCopyToSpecificArray{
 public static void main(String args[]){
  Set<Double> mySet = new TreeSet<> ();
  Integer myArrayParam[] = new Integer[10];
  
  /* Add Elements to the Set */
  mySet.add(55.6);
  mySet.add(10.01);
  
  /* Copy Elements to the myArray of type Double*/
  mySet.toArray(myArrayParam);
 }
}

Output

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Double
        at java.util.AbstractCollection.toArray(Unknown Source)
        at SetCopyToSpecificArray.main(SetCopyToSpecificArray.java:12)

Set is of type Double, but the Array which is passes to the toArray method is of type Integer, which is not a sub class of Double, So ArrayStoreException thrown.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment