Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6397444

Support for Generic arrays

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 6
    • tools
    • x86
    • windows_xp

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

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: