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

Inner class Arrays.ArrayList does not override and implement the method remove

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      When we use, the method Arrays.asList(), we are getting a list of type ArrayList which is an inner class of the class Arrays and not of type java.util.ArrayList. It's confusing but correct.

      The problem is the following, when we execute the methode remove or clear of this inner class, we receive an exception of type "UnsupportedOperationException". When I checked the implementation, I didn't find any override of the methode remove. In this case, the method remove of the class "java.util.AbstractList" is invoked as ArrayList is a subclass of AbstractList. And this method by default throw an exception.

      So an implementation of the method remove should be provided in that inner class.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Let's execute simply this line :

      Arrays.asList(1, 2, 3).clear();

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      This instruction should remove simply the elements in the list.
      ACTUAL -
      Exception in thread "main" java.lang.UnsupportedOperationException
      at java.base/java.util.AbstractList.remove(AbstractList.java:167)
      at java.base/java.util.AbstractList$Itr.remove(AbstractList.java:387)
      at java.base/java.util.AbstractList.removeRange(AbstractList.java:598)
      at java.base/java.util.AbstractList.clear(AbstractList.java:243)

      CUSTOMER SUBMITTED WORKAROUND :
      as a work around, we should use the ArrayList (java.util.ArrayList) of the package java.util, we can make the following :

              final List<Integer> integers = new ArrayList<>();
              integers.add(1);
              integers.add(2);
              integers.add(3);
              integers.clear();

      FREQUENCY : always


            tongwan Andrew Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: