mardi 1 août 2017

How do I pass generic type for java.lang.reflect.Array.newInstance?

My code snippet.

public class GenericArray <T extends Comparable<T>>{
   private T[] a;
   private int nElems;
   public  GenericArray(int max) // constructor
   {
       a=(T[])java.lang.reflect.Array.newInstance(Integer.class, max); 
       //Hard coded for Integer.
   }
}

GenericArray arr= new GenericArray<Integer>(100); 

This works, As I am instantiating explicitly passing Integer.class. How do I make this as generic ?

Or Is there a way to print Type information which passed to GenericArray using ParameterizedType ? (If I get that probably, I would handle using if statements)





Aucun commentaire:

Enregistrer un commentaire