Open
Description
The following code is not accepted by 24-th test in ArrayListTest. But this code works just fine.
@Override
@SuppressWarnings("unchecked")
public T remove(int index) {
Objects.checkIndex(index, size);
T removedElement = (T) elements[index];
Object[] copy = new Object[elements.length];
System.arraycopy(elements, 0, copy, 0, index);
System.arraycopy(elements, index + 1, copy, index, size - index - 1);
elements = copy;
size--;
return removedElement;
}
It works perfectly, if you just move internalArray initialization down like this:
@Test
@Order(24)
void removeElementByIndex() {
fillTestArray(15, 69, 58, 78, 100);
int removedElement = arrayList.remove(2);
Object[] internalArray = getTestArray();
assertThat(internalArray[2]).isEqualTo(78);
assertThat(internalArray[1]).isEqualTo(69);
assertThat(getTestSize()).isEqualTo(4);
assertThat(removedElement).isEqualTo(58);
}
Metadata
Metadata
Assignees
Labels
No labels