Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
public class ArrayList<T> implements List<T> {

public static final int DEFAULT_CAPACITY = 5;
private static final int DEFAULT_CAPACITY = 5;
private Object[] elementData;
private int size;

Expand Down Expand Up @@ -53,7 +53,7 @@ public static <T> List<T> of(T... elements) {
}

/**
* Adds an element to the array and returns index of position.
* Adds an element to the array.
*
* @param element element to add
*/
Expand All @@ -65,8 +65,8 @@ public void add(T element) {
}

private void increaseDataArrayIfFull() {
if (elementData.length <= size) {
elementData = getTrimmedArrayToSize(elementData.length * 2);
if (elementData.length == size) {
elementData = Arrays.copyOf(elementData, size * 2);
}
}

Expand Down Expand Up @@ -146,15 +146,16 @@ public void set(int index, T element) {
* throws {@link IndexOutOfBoundsException}
*
* @param index element index
* @return deleted element
*/
@Override
public void remove(int index) {
if (index == size - 1) {
elementData = getTrimmedArrayToSize(size - 1);
} else {
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
}
@SuppressWarnings("unchecked")
public T remove(int index) {
Objects.checkIndex(index, size);
T deletedElement = (T) elementData[index];
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
size--;
return deletedElement;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,25 @@ private void checkElementsExist() {
* throws {@link IndexOutOfBoundsException}
*
* @param index element index
* @return an element value
* @return deleted element
*/
@Override
public void remove(int index) {
public T remove(int index) {
T deletedElement = null;
if (index == 0) {
Objects.checkIndex(index, size);
deletedElement = head.value;
removeHead();
} else {
Node<T> previousNode = findNodeByIndex(index - 1);
deletedElement = previousNode.next.value;
previousNode.next = previousNode.next.next;
if (index == size - 1) {
tail = previousNode;
}
}
size--;
return deletedElement;
}

private void removeHead() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface List<E> {

E getLast();

void remove(int index);
E remove(int index);

boolean contains(E element);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,12 @@ public void setFirstElementOnEmptyTree() {
public void removeElementByIndex() {
arrayList = ArrayList.of(15, 69, 58, 78, 100);

arrayList.remove(2);
int removedElement = arrayList.remove(2);

assertThat(arrayList.get(2)).isEqualTo(78);
assertThat(arrayList.get(1)).isEqualTo(69);
assertThat(arrayList.size()).isEqualTo(4);
assertThat(removedElement).isEqualTo(58);
}

@Test
Expand All @@ -274,10 +275,11 @@ public void removeElementByIndexEqualToSize() {
public void removeLastElementByIndex() {
arrayList = ArrayList.of(15, 69, 58, 78, 100);

arrayList.remove(4);
int removedElement = arrayList.remove(4);

assertThat(arrayList.get(3)).isEqualTo(78);
assertThat(arrayList.size()).isEqualTo(4);
assertThat(removedElement).isEqualTo(100);
assertThatExceptionOfType(IndexOutOfBoundsException.class)
.isThrownBy(() -> arrayList.get(4));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

import java.util.NoSuchElementException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LinkedListTest {
Expand Down Expand Up @@ -76,7 +73,9 @@ void testGetLastElement() {

@Test
@Order(7)
void testGetFirstOfEmptyList() { assertThrows(NoSuchElementException.class, () -> intList.getFirst()); }
void testGetFirstOfEmptyList() {
assertThrows(NoSuchElementException.class, () -> intList.getFirst());
}

@Test
@Order(8)
Expand Down Expand Up @@ -202,7 +201,6 @@ void testSetElementByIndex() {
assertEquals(78, intList.get(1).intValue());
assertEquals(8, intList.get(3).intValue());
assertEquals(4, intList.size());

}

@Test
Expand Down Expand Up @@ -235,21 +233,23 @@ void testRemoveElementFromEmptyList() {
void testRemoveFirstElement() {
intList = LinkedList.of(4, 6, 8, 9);

intList.remove(0);
int deletedElement = intList.remove(0);

assertEquals(6, intList.get(0).intValue());
assertEquals(3, intList.size());
assertEquals(4, deletedElement);
}

@Test
@Order(25)
void testRemoveLastElement() {
intList = LinkedList.of(4, 6, 8, 9);

intList.remove(intList.size() - 1);
int deletedElement = intList.remove(intList.size() - 1);

assertEquals(8, intList.get(intList.size() - 1).intValue());
assertEquals(3, intList.size());
assertEquals(9, deletedElement);
}

@Test
Expand All @@ -258,10 +258,11 @@ void testRemoveElement() {
intList = LinkedList.of(1, 2, 3, 4, 5);

int elementIndex = 2;
intList.remove(elementIndex); // element = 3
int deletedElement = intList.remove(elementIndex); // element = 3

assertEquals(4, intList.get(elementIndex).intValue());
assertEquals(4, intList.size());
assertEquals(3, deletedElement);
}

@Test
Expand Down