src/share/classes/java/util/ArrayList.java

Print this page




 592         int numMoved = size - index;
 593         if (numMoved > 0)
 594             System.arraycopy(elementData, index, elementData, index + numNew,
 595                              numMoved);
 596 
 597         System.arraycopy(a, 0, elementData, index, numNew);
 598         size += numNew;
 599         return numNew != 0;
 600     }
 601 
 602     /**
 603      * Removes from this list all of the elements whose index is between
 604      * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
 605      * Shifts any succeeding elements to the left (reduces their index).
 606      * This call shortens the list by {@code (toIndex - fromIndex)} elements.
 607      * (If {@code toIndex==fromIndex}, this operation has no effect.)
 608      *
 609      * @throws IndexOutOfBoundsException if {@code fromIndex} or
 610      *         {@code toIndex} is out of range
 611      *         ({@code fromIndex < 0 ||
 612      *          fromIndex >= size() ||
 613      *          toIndex > size() ||
 614      *          toIndex < fromIndex})
 615      */
 616     protected void removeRange(int fromIndex, int toIndex) {




 617         modCount++;
 618         int numMoved = size - toIndex;
 619         System.arraycopy(elementData, toIndex, elementData, fromIndex,
 620                          numMoved);
 621 
 622         // clear to let GC do its work
 623         int newSize = size - (toIndex-fromIndex);
 624         for (int i = newSize; i < size; i++) {
 625             elementData[i] = null;
 626         }
 627         size = newSize;
 628     }
 629 
 630     /**
 631      * Checks if the given index is in range.  If not, throws an appropriate
 632      * runtime exception.  This method does *not* check if the index is
 633      * negative: It is always used immediately prior to an array access,
 634      * which throws an ArrayIndexOutOfBoundsException if index is negative.
 635      */
 636     private void rangeCheck(int index) {
 637         if (index >= size)
 638             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 639     }
 640 
 641     /**
 642      * A version of rangeCheck used by add and addAll.
 643      */
 644     private void rangeCheckForAdd(int index) {
 645         if (index > size || index < 0)
 646             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 647     }
 648 
 649     /**
 650      * Constructs an IndexOutOfBoundsException detail message.
 651      * Of the many possible refactorings of the error handling code,
 652      * this "outlining" performs best with both server and client VMs.
 653      */
 654     private String outOfBoundsMsg(int index) {
 655         return "Index: "+index+", Size: "+size;







 656     }
 657 
 658     /**
 659      * Removes from this list all of its elements that are contained in the
 660      * specified collection.
 661      *
 662      * @param c collection containing elements to be removed from this list
 663      * @return {@code true} if this list changed as a result of the call
 664      * @throws ClassCastException if the class of an element of this list
 665      *         is incompatible with the specified collection
 666      * (<a href="Collection.html#optional-restrictions">optional</a>)
 667      * @throws NullPointerException if this list contains a null element and the
 668      *         specified collection does not permit null elements
 669      * (<a href="Collection.html#optional-restrictions">optional</a>),
 670      *         or if the specified collection is null
 671      * @see Collection#contains(Object)
 672      */
 673     public boolean removeAll(Collection<?> c) {
 674         Objects.requireNonNull(c);
 675         return batchRemove(c, false);




 592         int numMoved = size - index;
 593         if (numMoved > 0)
 594             System.arraycopy(elementData, index, elementData, index + numNew,
 595                              numMoved);
 596 
 597         System.arraycopy(a, 0, elementData, index, numNew);
 598         size += numNew;
 599         return numNew != 0;
 600     }
 601 
 602     /**
 603      * Removes from this list all of the elements whose index is between
 604      * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
 605      * Shifts any succeeding elements to the left (reduces their index).
 606      * This call shortens the list by {@code (toIndex - fromIndex)} elements.
 607      * (If {@code toIndex==fromIndex}, this operation has no effect.)
 608      *
 609      * @throws IndexOutOfBoundsException if {@code fromIndex} or
 610      *         {@code toIndex} is out of range
 611      *         ({@code fromIndex < 0 ||

 612      *          toIndex > size() ||
 613      *          toIndex < fromIndex})
 614      */
 615     protected void removeRange(int fromIndex, int toIndex) {
 616         if (fromIndex > toIndex) {
 617             throw new IndexOutOfBoundsException(
 618                     outOfBoundsMsg(fromIndex, toIndex));
 619         }
 620         modCount++;
 621         int numMoved = size - toIndex;
 622         System.arraycopy(elementData, toIndex, elementData, fromIndex,
 623                          numMoved);
 624 
 625         // clear to let GC do its work
 626         int newSize = size - (toIndex-fromIndex);
 627         for (int i = newSize; i < size; i++) {
 628             elementData[i] = null;
 629         }
 630         size = newSize;
 631     }
 632 
 633     /**
 634      * Checks if the given index is in range.  If not, throws an appropriate
 635      * runtime exception.  This method does *not* check if the index is
 636      * negative: It is always used immediately prior to an array access,
 637      * which throws an ArrayIndexOutOfBoundsException if index is negative.
 638      */
 639     private void rangeCheck(int index) {
 640         if (index >= size)
 641             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 642     }
 643 
 644     /**
 645      * A version of rangeCheck used by add and addAll.
 646      */
 647     private void rangeCheckForAdd(int index) {
 648         if (index > size || index < 0)
 649             throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 650     }
 651 
 652     /**
 653      * Constructs an IndexOutOfBoundsException detail message.
 654      * Of the many possible refactorings of the error handling code,
 655      * this "outlining" performs best with both server and client VMs.
 656      */
 657     private String outOfBoundsMsg(int index) {
 658         return "Index: "+index+", Size: "+size;
 659     }
 660 
 661     /**
 662      * A version used in checking (fromIndex > toIndex) condition
 663      */
 664     private static String outOfBoundsMsg(int fromIndex, int toIndex) {
 665         return "From Index: " + fromIndex + " > To Index: " + toIndex;
 666     }
 667 
 668     /**
 669      * Removes from this list all of its elements that are contained in the
 670      * specified collection.
 671      *
 672      * @param c collection containing elements to be removed from this list
 673      * @return {@code true} if this list changed as a result of the call
 674      * @throws ClassCastException if the class of an element of this list
 675      *         is incompatible with the specified collection
 676      * (<a href="Collection.html#optional-restrictions">optional</a>)
 677      * @throws NullPointerException if this list contains a null element and the
 678      *         specified collection does not permit null elements
 679      * (<a href="Collection.html#optional-restrictions">optional</a>),
 680      *         or if the specified collection is null
 681      * @see Collection#contains(Object)
 682      */
 683     public boolean removeAll(Collection<?> c) {
 684         Objects.requireNonNull(c);
 685         return batchRemove(c, false);