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

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u51
    • 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[]

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

              Created:
              Updated:
              Resolved: