A difficult question which I\'m close to giving up all hope on. I\'m trying to make a
function, but am having problems getting ArrayList.toArray()
to return the ty
You can't create an array of a generic type parameter. The simple reason is the fact that java usually erases the generic type information at runtime so the runtime has no idea what T is (when T is a generic type parameter).
So the solution is to actually get a hold of the type information at runtime. Usually this is done by passing around a Class
parameter but in this case you can avoid that:
@peter's answer looks good and i would use it :).
return (T[]) list.toArray(Array.newInstance(one.getClass(), list.size()));