Generici Java - Nessun array

Non sono consentiti array di tipi parametrizzati.

//Cannot create a generic array of Box<Integer>
Box<Integer>[] arrayOfLists = new Box<Integer>[2];

Poiché il compilatore utilizza la cancellazione del tipo, il parametro del tipo viene sostituito con Object e l'utente può aggiungere qualsiasi tipo di oggetto all'array. E in fase di esecuzione, il codice non sarà in grado di generare ArrayStoreException.

// compiler error, but if it is allowed
Object[] stringBoxes = new Box<String>[];
  
// OK
stringBoxes[0] = new Box<String>();  

// An ArrayStoreException should be thrown,
//but the runtime can't detect it.
stringBoxes[1] = new Box<Integer>();