generics - How to get class of T from Vector<T> in java -


this question has answer here:

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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -