mirror of
https://github.com/corda/corda.git
synced 2025-01-05 20:54:13 +00:00
Fix an off-by-1 error in the remove method.
The change to only grow the array when the capacity has been reached exposed a bug in the remove method when shifting the array elements.
This commit is contained in:
parent
8706b6ad4a
commit
26209efac2
@ -131,13 +131,14 @@ public class ArrayList<T> extends AbstractList<T> implements java.io.Serializabl
|
|||||||
public T remove(int index) {
|
public T remove(int index) {
|
||||||
T v = get(index);
|
T v = get(index);
|
||||||
|
|
||||||
if (index == size - 1) {
|
int newSize = size - 1;
|
||||||
|
|
||||||
|
if (index == newSize) {
|
||||||
array[index] = null;
|
array[index] = null;
|
||||||
} else {
|
} else {
|
||||||
System.arraycopy(array, index + 1, array, index, size - index);
|
System.arraycopy(array, index + 1, array, index, newSize - index);
|
||||||
}
|
}
|
||||||
|
|
||||||
int newSize = size - 1;
|
|
||||||
shrink(newSize);
|
shrink(newSize);
|
||||||
size = newSize;
|
size = newSize;
|
||||||
|
|
||||||
|
@ -145,6 +145,24 @@ public class List {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void testRemove() {
|
||||||
|
ArrayList<String> foo = new ArrayList<String>(2);
|
||||||
|
foo.add("Uno");
|
||||||
|
foo.add("Dos");
|
||||||
|
foo.add("Tres");
|
||||||
|
foo.add("Cuatro");
|
||||||
|
|
||||||
|
ArrayList<String> fooToRemove = new ArrayList<String>(2);
|
||||||
|
fooToRemove.add(foo.get(0));
|
||||||
|
fooToRemove.add(foo.get(1));
|
||||||
|
|
||||||
|
for (String s : fooToRemove) {
|
||||||
|
foo.remove(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(foo.size() == 2);
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String args[]) {
|
public static void main(String args[]) {
|
||||||
ArrayList<Integer> l = new ArrayList<Integer>();
|
ArrayList<Integer> l = new ArrayList<Integer>();
|
||||||
l.add(1); l.add(2); l.add(3); l.add(4); l.add(5);
|
l.add(1); l.add(2); l.add(3); l.add(4); l.add(5);
|
||||||
@ -170,5 +188,6 @@ public class List {
|
|||||||
testIterators2(new ArrayList());
|
testIterators2(new ArrayList());
|
||||||
testIterators2(new LinkedList());
|
testIterators2(new LinkedList());
|
||||||
testGrow();
|
testGrow();
|
||||||
|
testRemove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user