use fromIndex and toIndex for remove instead of from and length

The new API is consistent with the internal API of ArrayList.
This commit is contained in:
2017-09-29 18:12:58 +02:00
parent 5112fcdfa0
commit fc1ca26d52
2 changed files with 36 additions and 30 deletions

View File

@@ -198,31 +198,31 @@ public class IntList implements Serializable, Cloneable {
/**
* Removes elements from the list.
*
* @param from
* @param fromIndex
* index of the first element to remove
* @param length
* number of elements to remove
* @param toIndex
* the index of the last element to remove (exclusive)
* @throws IndexOutOfBoundsException
* if {@code from} or {@code length} is negative, or if the range
* defined by {@code from} and {@code length} is out of bounds
* if {@code fromIndex} or {@code toIndex} is negative, or if the
* range defined by {@code fromIndex} and {@code toIndex} is out of
* bounds
*/
public void remove(final int from, final int length) {
public void remove(final int fromIndex, final int toIndex) {
if (from < 0) {
throw new IndexOutOfBoundsException("from must not be negative, but was: " + from);
if (fromIndex < 0) {
throw new IndexOutOfBoundsException("from must not be negative, but was: " + fromIndex);
}
if (length < 0) {
throw new IndexOutOfBoundsException("length must not be negative, but was: " + length);
if (toIndex < fromIndex) {
throw new IndexOutOfBoundsException("toIndex must not be smaller than fromIndex, but was: " + toIndex);
}
if (from + length > index) {
throw new IndexOutOfBoundsException("from: " + from + " length: " + length);
if (toIndex > index) {
throw new IndexOutOfBoundsException("from: " + fromIndex + " toIndex: " + toIndex);
}
final int[] newData = new int[data.length];
System.arraycopy(data, 0, newData, 0, from);
System.arraycopy(data, from + length, newData, from, data.length - from - length);
data = newData;
index -= length;
final int numRemoved = index - toIndex;
System.arraycopy(data, toIndex, data, fromIndex, numRemoved);
index = index - (toIndex - fromIndex);
}
/**

View File

@@ -295,25 +295,31 @@ public class IntListTest {
list.addAll(ints);
list.remove(2, 0);
// remove nothing
list.remove(2, 2);
Assert.assertArrayEquals(ints, list.toArray());
list.remove(9, 1);
// remove the last element
list.remove(9, 10);
final int[] expectedA = removeElements(ints, 9);
Assert.assertArrayEquals(expectedA, list.toArray());
// remove the first element
list.remove(0, 1);
final int[] expectedB = removeElements(expectedA, 0);
Assert.assertArrayEquals(expectedB, list.toArray());
list.remove(7, 1);
final int[] expectedC = removeElements(expectedB, 7);
// remove single element in the middle
list.remove(3, 4);
final int[] expectedC = removeElements(expectedB, 3);
Assert.assertArrayEquals(expectedC, list.toArray());
list.remove(3, 3);
final int[] expectedD = removeElements(expectedC, 3, 4, 5);
// remove several elements in the middle
list.remove(2, 5);
final int[] expectedD = removeElements(expectedC, 2, 3, 4);
Assert.assertArrayEquals(expectedD, list.toArray());
// remove all elements
list.remove(0, 4);
final int[] expectedE = removeElements(expectedD, 0, 1, 2, 3);
Assert.assertArrayEquals(expectedE, list.toArray());
@@ -332,19 +338,19 @@ public class IntListTest {
// expected
}
try {
list.remove(1, -1);
list.remove(1, 0);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(3, 2);
list.remove(3, 5);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
}
try {
list.remove(4, 1);
list.remove(4, 5);
Assert.fail();
} catch (final IndexOutOfBoundsException e) {
// expected
@@ -387,7 +393,7 @@ public class IntListTest {
Assert.assertEquals(a.hashCode(), b.hashCode());
// change one value
a.remove(2, 1);
a.remove(2, 3);
a.insert(2, 99);
Assert.assertFalse(a.equals(b));
Assert.assertNotEquals(a.hashCode(), b.hashCode());
@@ -404,9 +410,9 @@ public class IntListTest {
Assert.assertArrayEquals(expectedE, list.toArray());
}
private int[] removeElements(final int[] data, final int... removedElements) {
final int[] result = new int[data.length - removedElements.length];
final List<Integer> blacklist = Arrays.stream(removedElements).boxed().collect(Collectors.toList());
private int[] removeElements(final int[] data, final int... removedIndices) {
final int[] result = new int[data.length - removedIndices.length];
final List<Integer> blacklist = Arrays.stream(removedIndices).boxed().collect(Collectors.toList());
int j = 0;
for (int i = 0; i < data.length; i++) {