United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6397444 Support for Generic arrays
JDK-6397444 : Support for Generic arrays

Details
Type:
Enhancement
Submit Date:
2006-03-13
Status:
Closed
Updated Date:
2011-02-16
Project Name:
JDK
Resolved Date:
2006-03-13
Component:
tools
OS:
windows_xp
Sub-Component:
javac
CPU:
x86
Priority:
P5
Resolution:
Duplicate
Affected Versions:
6
Fixed Versions:

Related Reports
Duplicate:

Sub Tasks

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>[]

                                    

Comments



Hardware and Software, Engineered to Work Together