JDK-6397444 : Support for Generic arrays
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-03-13
  • Updated: 2011-02-16
  • Resolved: 2006-03-13
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
Generic arrays  new List<Object>[10] are not suported.
Section 7.3 http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf explanes why, but I belive it is a weak argument.  Here is an example of a runtime type validation that works in java.

String[] names = new String[1];
Object[] objects = names;
 objects[0] = new Integer(1);//runtime error incorrect type





JUSTIFICATION :
It is just not intuitive. I am sure this problem came up before and I sure it will come up again and again.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
That i can declear generic array


ACTUAL -
Does not compile

---------- BEGIN SOURCE ----------
/*
 * Example1.java
 *
 * Created on March 10, 2006, 1:12 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

import java.util.*;

/**
 *
 * @author JoeyS
 */
public class Example1 {
    
     public static final void main(String[] args){
        
        //compile but causes a runtime error
        String[] names = new String[1];
        Object[] objects = names;
        objects[0] = new Integer(1);//runtime error incorrect type
        
        //example from section 7.3 http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
        //if type information was kept at runtime then would work this
        List<String>[] lsa = new List<String>[10]; // not really allowed
        Object o = lsa;
        Object[] oa = (Object[]) o;
        List<Integer> li = new ArrayList<Integer>();
        li.add(new Integer(3));
        oa[1] = li; // unsound, but passes run time store check
        String s = lsa[1].get(0); // run-time error - ClassCastException
    }
    
    
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
use a List<List<Object>> instead of a List<Object>[]