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

Allow the creation of Generic type Arrays

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 7
    • specification
    • x86
    • windows_7

      A DESCRIPTION OF THE REQUEST :
      It is impossible to write programs/classes that implement generic type arrays.
      For exemple, we can't make such a class:

      public class myGenericClass<T> {
          private T[] genericArray;

          public myGenericClass (int length) {
              T = new T[length]; // genereic type arrays not allowed
          }
      }

      JUSTIFICATION :
      It is really frustrating !

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect the following line of code to be valid:
      T = new T[length];
      ACTUAL -
      The following line of code is not valid:
      T = new T[length];

      ---------- BEGIN SOURCE ----------
      public class myGenericClass<T> {
          private T[] genericArray;

          public myGenericClass (int length) {
              T = new T[length]; // genereic type arrays not allowed
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I don't like this "workaround", but here it is:

      public class myGenericClass<T> {
          private T[] genericArray;

          public myGenericClass (int length) {
              T = (T[])new Object[length]; // casting an Object[] to a T[]
          }
      }

            abuckley Alex Buckley
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: