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

Arrays.asList methods needs better documentation

    XMLWordPrintable

    Details

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0_23"
      Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
      Java HotSpot(TM) 64-Bit Server VM (build 19.0-b09, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.0.6002]

      A DESCRIPTION OF THE PROBLEM :
      In java.util.Arrays class, a method called "asList" creates a list out of a given array. HOWEVER, the documentation fails to mention that the underlying list does not support "add" or "remove" operations that regular lists do.

      Second problem is that it returns an ArrayList object, HOWEVER, this object is not the same as the regular java.util.ArrayList object but is internal to this class.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Create new list from array list
      Try "list.add" or "list.remove"

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The operation should work

      ACTUAL -
      An error is returned

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Stacktrace:
      java.lang.UnsupportedOperationException
      at java.util.AbstractList.remove(AbstractList.java:144)
      at java.util.AbstractList$Itr.remove(AbstractList.java:360)
      at java.util.AbstractList.removeRange(AbstractList.java:559)
      at java.util.AbstractList.clear(AbstractList.java:217)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      List list = Arrays.asList(1, 2, 3)
      list.remove(1);
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Wrap the returned object in a new linked list as follows:

      List list = new LinkedList(Arrays.asList(1, 2, 3));

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              smarks Stuart Marks
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: