RFR [8014066] Mistake in documentation of ArrayList#removeRange

Ulf Zibis Ulf.Zibis at CoSoCo.de
Tue Mar 18 22:12:26 UTC 2014


Am 18.03.2014 19:28, schrieb Ivan Gerasimov:
>
> Assuming this last iteration is OK, should the next step be a CCC request?

Do you mean? :
      /*
       * ...
+     * It is assumed that fromIndex <= toIndex, otherwise the behaviour of this method is undefined.
       * ...
  -     *          toIndex < fromIndex})
       * ...
      */
      protected void removeRange(int fromIndex, int toIndex) {
          ...

Please remove and replace inline by size - toIndex:
  621         int numMoved = size - toIndex;


About modCount:

Wouldn't it be more correct to code? :

  616         if (fromIndex > toIndex) {
  617             throw new IndexOutOfBoundsException(
  618                     outOfBoundsMsg(fromIndex, toIndex));
  619         }
  620         try {
  621             modCount++;
  622             System.arraycopy(elementData, toIndex, elementData, fromIndex,
  623                     size - toIndex);
  624         } catch (IndexOutOfBoundsException ioobe) {
  625             modCount--;
  626             throw ioobe;
  627         }

Of even better :

  000     private int[] modCount = { 0 };
  ...
  616         if (fromIndex > toIndex) {
  617             throw new IndexOutOfBoundsException(
  618                     outOfBoundsMsg(fromIndex, toIndex));
  619         }
  620         System.arraycopy(elementData, toIndex, elementData, fromIndex,
  621                 size - toIndex, modCount);

Or :

  000 public class ArrayList ... implements ModCounter {
  001     private int modCount = 0;
  001     void incModCount() {
  001         modCount++;
  004    }
  ...
  616         if (fromIndex > toIndex) {
  617             throw new IndexOutOfBoundsException(
  618                     outOfBoundsMsg(fromIndex, toIndex));
  619         }
  620         System.arraycopy(elementData, toIndex, elementData, fromIndex,
  621                 size - toIndex, this);

-Ulf





More information about the core-libs-dev mailing list