// 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. 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 cast ls[0] = new ArrayList<>(); // -ls[1] = new ArrayList(); // error: incompatible types: ArrayList // cannot be converted to List // 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 cast" warning: List[] spheres = (List[])new List[10]; Arrays.setAll(spheres, n -> new ArrayList<>()); } }