www.pudn.com > ThinkinginJava4thEdition(SourceCode).zip > ArrayOfGenerics.java


//: arrays/ArrayOfGenerics.java 
// 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(); 
  } 
} ///:~