Unfortunately, I don’t believe there really is a better way of doing this due to the nature of Java’s handling of primitive types, boxing, arrays and generics. In particular:
List<T>.toArray
won’t work because there’s no conversion fromInteger
toint
- You can’t use
int
as a type argument for generics, so it would have to be anint
-specific method (or one which used reflection to do nasty trickery).
I believe there are libraries which have autogenerated versions of this kind of method for all the primitive types (i.e. there’s a template which is copied for each type). It’s ugly, but that’s the way it is I’m afraid 🙁
Even though the Arrays
class came out before generics arrived in Java, it would still have to include all the horrible overloads if it were introduced today (assuming you want to use primitive arrays).