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

it.next on ArrayList throws wrong type of Exception after remove(-1)

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P5
    • tbd
    • 8u45, 9
    • core-libs

    Description

      FULL PRODUCT VERSION :
      /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java -version
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin amgdh077.o.aist.go.jp 13.3.0 Darwin Kernel Version 13.3.0: Tue Jun 3 21:27:35 PDT 2014; root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64 i386 MacPro6,1 Darwin


      A DESCRIPTION OF THE PROBLEM :
      (1) create new ArrayList
      (2) get iterator on list
      (3) call remove(-1) on list -> IndexOutOfBoundsException
      (4) call iterator.next

      Result: ConcurrentModification even though the list was never modified (it was always empty). Should be: NoSuchElementException.

      Result is correct if remove(0) is called, or if LinkedList is used.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See source code`

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      NoSuchElementException
      ACTUAL -
      ConcurrentModificationException

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.ArrayList;
      import java.util.LinkedList;
      import java.util.ConcurrentModificationException;
      import java.util.Iterator;
      import java.util.NoSuchElementException;

      public class iterator_test {
        public static void main(String[] argv) {
          /* BUG: Sequence of arrayList.iterator, arrayList.remove(-1), iterator.next
             produces wrong type of Exception. */
          ArrayList<Integer> testArrayList = new ArrayList<Integer>();
          Iterator<Integer> it = testArrayList.iterator();
          try {
            testArrayList.remove(-1);
          } catch (IndexOutOfBoundsException e) {
          }
          try {
            it.next();
          } catch (ConcurrentModificationException e) {
            System.err.println("Should be NoSuchElementException!");
          }

          /* Correct result with remove(0) */
          testArrayList = new ArrayList<Integer>();
          it = testArrayList.iterator();
          try {
            testArrayList.remove(0);
          } catch (IndexOutOfBoundsException e) {
          }
          try {
            it.next();
          } catch (NoSuchElementException e) {
          }

          /* Correct result with remove(-1) on LinkedList */
          LinkedList<Integer> testLinkedList = new LinkedList<Integer>();
          it = testLinkedList.iterator();
          try {
            testLinkedList.remove(-1);
          } catch (IndexOutOfBoundsException e) {
          }
          try {
            it.next();
          } catch (NoSuchElementException e) {
          }
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Not available.

      Attachments

        Issue Links

          Activity

            People

              martin Martin Buchholz
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated: