diff --git a/src/test/java/core/basesyntax/ArrayList.java b/src/test/java/core/basesyntax/ArrayList.java new file mode 100644 index 000000000..c01f7fc76 --- /dev/null +++ b/src/test/java/core/basesyntax/ArrayList.java @@ -0,0 +1,95 @@ +package core.basesyntax; + +import core.basesyntax.ArrayListIndexOutOfBoundsException; +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList { + private static final int DEFAULT_CAPACITY = 10; + private E[] elements; + private int size; + + public ArrayList() { + elements = (E[]) new Object[DEFAULT_CAPACITY]; + size = 0; + } + + public void add(E value) { + ensureCapacity(); + elements[size++] = value; + } + + public void add(E value, int index) { + validateIndexForAdd(index); + ensureCapacity(); + System.arraycopy(elements, index, elements, index + 1, size - index); + elements[index] = value; + size++; + } + + public void addAll(ArrayList list) { + if (list == null || list.isEmpty()) { + return; + } + + for (int i = 0; i < list.size; i++) { + add(list.get(i)); + } + } + + public E remove(int index) { + validateIndex(index); + E removedElement = elements[index]; + System.arraycopy(elements, index + 1, elements, index, size - index - 1); + elements[--size] = null; + + return removedElement; + } + + public E remove(E value) { + for (int i = 0; i < size; i++) { + if ((value == null && elements[i] == null) || (value != null && value.equals(elements[i]))) { + return remove(i); + } + } + + throw new NoSuchElementException("Element not found: " + value); + } + + public E get(int index) { + validateIndex(index); + + return elements[index]; + } + + public void set(E value, int index) { + validateIndex(index); + elements[index] = value; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + private void ensureCapacity() { + if (size == elements.length) { + elements = Arrays.copyOf(elements, elements.length * 2); + } + } + + private void validateIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException("Index out of bounds: " + index); + } + } + + private void validateIndexForAdd(int index) { + if (index < 0 || index > size) { + throw new ArrayListIndexOutOfBoundsException("Index out of bounds for add: " + index); + } + } +} diff --git a/src/test/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java b/src/test/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java new file mode 100644 index 000000000..d39fbe402 --- /dev/null +++ b/src/test/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java @@ -0,0 +1,7 @@ +package core.basesyntax; + +public class ArrayListIndexOutOfBoundsException extends RuntimeException { + public ArrayListIndexOutOfBoundsException(String message) { + super(message); + } +}