Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java generics and arrays construction

Suppose I have a generic class with a generic parameter T which is a Number subclass. I would like to initialize an array of T during class construction. Is it possible? If yes how? If not why?

public class AClass<T extends Number>{

    private T array[];
    private int arrayOfInt[];

    public AClass(int size){
        arrayOfInt = new int[size];
        array = ? //what should I put here?
    } 
} 
like image 255
Heisenbug Avatar asked Aug 28 '11 16:08

Heisenbug


People also ask

Can we use generics with array in Java?

Java allows generic classes, methods, etc. that can be declared independent of types. However, Java does not allow the array to be generic. The reason for this is that in Java, arrays contain information related to their components and this information is used to allocate memory at runtime.

How do you implement a generic array in Java?

You can do this: E[] arr = (E[])new Object[INITIAL_ARRAY_LENGTH]; This is one of the suggested ways of implementing a generic collection in Effective Java; Item 26. No type errors, no need to cast the array repeatedly.

How do you create an array in generic class?

The first parameter specifies the type of object inside the new array. The second parameter specifies how much space to create for the array. As the result of Array#newInstance is of type Object, we need to cast it to E[] to create our generic array.


3 Answers

T is only know at compile time. It is not know at runtime and thus you cannot initilise the contents of the array. However you can create the array, every value will be null.

array = (T[]) new Number[size];

EDIT: The problem with creating instances of any type is you need to know what is the default value you want and which constructor you want to call. e.g. there is no new Double()

As mentioned below, double[] will be more efficient and faster than Number[] and unless you need large long values, it will be able to store every possible value.

like image 131
Peter Lawrey Avatar answered Oct 15 '22 17:10

Peter Lawrey


If you want to use arrays, there are two options:

  1. 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.

  2. 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.

like image 26
newacct Avatar answered Oct 15 '22 17:10

newacct


This is not possible.

Because Java generics use type erasure, the type of T isn't known at runtime, so you can't create an array of it.

like image 2
SLaks Avatar answered Oct 15 '22 17:10

SLaks