java - Creating a generic array instance in a generic method -


i'm trying build helper method turn 2 line list array conversion single line. problem i've ran i'm not sure how create a t[] instance.

i've tried

array.newinstance(t.class, list.size) can't feed t.class..

also tried new t[](list.size) doesn't parameters.

public <t> t[] converttoarray(list<t> list) {    t[] result = ???     result = list.toarray(result);     return result; } 

any other ideas?

thanks

you can't mix generics , arrays that. generics have compile-time checking, arrays have runtime checking, , approaches incompatible. @ first suggested this:

@suppresswarnings("unchecked") public <t> t[] converttoarray(list<t> list) {          object[] result = new object[list.size()];    result = list.toarray(result);    return (t[])result; } 

this wrong in stealthy way, @ least 1 other person on here thought work! when run incompatible type error, because can't cast object[] integer[]. why can't t.class , create array right type? or new t[]?

generics use type erasure preserve backward compatibility. checked @ compile time, stripped runtime, bytecode compatible pre-generics jvms. means cannot have class knowledge of generic variable @ runtime!

so while can guarantee t[] result of type integer[] ahead of time, code list.toarray(result); (or new t[], or array.newinstance(t.class, list.size());) happen @ runtime, , cannot know t is!

here's version does work, reward reading lecture:

public static <t> t[] converttoarray(list<?> list, class<t> c) {     @suppresswarnings("unchecked")     t[] result = (t[]) array.newinstance(c, list.size());     result = list.toarray(result);     return (t[]) result; } 

note have second parameter provide class @ runtime (as @ compile time via generics). use so:

integer[] arrayofintegers = converttoarray(listofintegers, integer.class); 

is worth hassle? still need suppress warning, safe?

my answer yes. warning generated there "i'm not sure" compiler. stepping through it, can confirm that cast succeed - if put wrong class in second parameter, compile-time warning thrown.

the major advantage of doing have centralised warning 1 single place. need prove 1 place correct, , know code succeed. quote java documentation:

the language designed guarantee if entire application has been compiled without unchecked warnings using javac -source 1.5, type safe[1]

so rather having these warnings on code, it's in 1 place, , can use without having worry - there's massively reduced risk of making mistake using it.

you may want @ this answer explains issue in more depth, , this answer crib sheet when writing this. already cited java documentation, handy reference used this blog post neal gafter, ex senior staff engineer @ sun microsystems , co-designer of 1.4 , 5.0's language features.

and of course, shanec rightly pointed out first answer failed @ runtime!


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -