generics - How to get class of T from Vector<T> in java -
this question has answer here:
- java: how class literal generic type? 7 answers
- how class instance of generics type t 14 answers
i wrote code:
public static <t> void getlist(vector<t> result){ system.out.println(result.getclass().getname()); }
i want write class name of t
, can't it. how can this?
as far know can't. java generics use type erasure, @ runtime vector<t>
behaves vector
without template arguments.
what can instead query type of element of vector.
here's short description of type erasure: http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
see answers question: java generics - type erasure - when , happens
in other words:
void somemethod(vector<t> values) { t value = values.get(0); }
is equivalent to:
void somemethod(vector values) { t value = (t) values.get(0); }
at runtime compile time checks type casting to.
Comments
Post a Comment