XMLWordPrintable

      The `Arrays.asList()` API returns an instance of `List`. Calling the `toArray()` method on that `List` instance is specified always to return `Object[]`, that is, an array of `Object`. In previous releases, it would sometimes return an array of some subtype. Note that the declared return type of `Collection.toArray()` is `Object[]`, which permits an instance of an array of a subtype to be returned. The specification wording, however, clearly requires an array of `Object` to be returned.

      The `toArray()` method has been changed to conform to the specification, and it now always returns `Object[]`. This may cause code that was expecting the old behavior to fail with a `ClassCastException`. An example of code that worked in previous releases but that now fails is the following:
      ```
          List<String> list = Arrays.asList("a", "b", "c");
          String[] array = (String[]) list.toArray();
      ```
      If this problem occurs, rewrite the code to use the one-arg form `toArray(T[])`, and provide an instance of the desired array type. This will also eliminate the need for a cast.
      ```
          String[] array = list.toArray(new String[0]);
      ```

            smarks Stuart Marks
            smarks Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: