If you want to use arrays, there are two options:
Peter Lawrey's answer, array = (T[]) new Number[size];
. You have to make sure never to return or pass this variable to code outside of the class that expect it to be an array of a particular type, which will cause an exception.
Declare array
as type Number[]
, then just do array = new Number[size];
. The downside of this is that when you get anything out of it you will need to explicitly cast to T
to use it as such.
The two are the same after type erasure, and they will both cause unchecked cast warning, so it's really a matter of personal preference. The former is more convenient, while the latter is more formally correct (you are not pretending it's a type it's not).
Alternately, some people will tell you to use an ArrayList<T>
instead. But internally, an ArrayList is still implemented using one of these two options.