ArrayList.toArray() should return an E[] not an Object[]

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P4
    • None
    • Affects Version/s: 8u51
    • Component/s: core-libs

      A DESCRIPTION OF THE REQUEST :
      ArrayList.toArray() currently returns an Object[], making it overly complicated to convert an ArrayList to an array.

      JUSTIFICATION :
      It is overly complicated to convert an ArrayList<String> to a String[] because the toArray method returns an Object[]

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Given an ArrayList<String>, stringList, stringList.toArray() should return a String[].
      ACTUAL -
      Given an ArrayList<String>, stringList, stringList.toArray() returns an Object[]

      ---------- BEGIN SOURCE ----------
      ArrayList<String> stringList = new ArrayList<String>();
      stringList.add("Hello");
      stringList.add("World");
      Object[] array = stringList.toArray(); // toArray returns an Object[] instead of a String[]
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Given an ArrayList<String>, stringList, calling stringList.toArray(new String[0]); returns a String[]

            Assignee:
            Unassigned
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: