diff options
Diffstat (limited to 'libjava/java/util/ArrayList.java')
-rw-r--r-- | libjava/java/util/ArrayList.java | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/libjava/java/util/ArrayList.java b/libjava/java/util/ArrayList.java index 34dc48ac179..99745d0f648 100644 --- a/libjava/java/util/ArrayList.java +++ b/libjava/java/util/ArrayList.java @@ -427,8 +427,8 @@ public class ArrayList extends AbstractList if (csize + size > data.length) ensureCapacity(size + csize); int end = index + csize; - if (index != size) - System.arraycopy(data, index, data, end, csize); + if (size > 0 && index != size) + System.arraycopy(data, index, data, end, size - index); size += csize; for ( ; index < end; index++) data[index] = itr.next(); @@ -437,19 +437,23 @@ public class ArrayList extends AbstractList /** * Removes all elements in the half-open interval [fromIndex, toIndex). - * You asked for it if you call this with invalid arguments. + * Does nothing when toIndex is equal to fromIndex. * * @param fromIndex the first index which will be removed * @param toIndex one greater than the last index which will be removed + * @throws IndexOutOfBoundsException if fromIndex > toIndex */ protected void removeRange(int fromIndex, int toIndex) { - if (fromIndex != toIndex) + int change = toIndex - fromIndex; + if (change > 0) { modCount++; System.arraycopy(data, toIndex, data, fromIndex, size - toIndex); - size -= toIndex - fromIndex; + size -= change; } + else if (change < 0) + throw new IndexOutOfBoundsException(); } /** |