// arrays/ArrayOfGenerics.java // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. // It is possible to create arrays of generics. import java.util.*; public class ArrayOfGenerics { @SuppressWarnings("unchecked") public static void main(String[] args) { List[] ls; List[] la = new List[10]; ls = (List[])la; // "Unchecked" warning ls[0] = new ArrayList<>(); // Compile-time checking produces an error: //- ls[1] = new ArrayList(); // The problem: List is a subtype of Object Object[] objects = ls; // So assignment is OK // Compiles and runs without complaint: objects[1] = new ArrayList<>(); // However, if your needs are straightforward it is // possible to create an array of generics, albeit // with an "unchecked" warning: List[] spheres = (List[])new List[10]; for(int i = 0; i < spheres.length; i++) spheres[i] = new ArrayList<>(); } }