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

View File

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