diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 8d6477943..577535be2 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,48 +1,121 @@ package core.basesyntax; +import java.util.NoSuchElementException; + public class ArrayList implements List { - @Override - public void add(T value) { + private static final Object[] EMPTY_ELEMENTDATA = {}; + private static final int DEFAULT_CAPACITY = 10; + private Object[] elementData; + private int size; + public ArrayList() { + this.elementData = EMPTY_ELEMENTDATA; + this.size = 0; } - @Override - public void add(T value, int index) { + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException("Index " + index + + " is out of bounds for size " + size); + } + } + + private void checkIndexForAdd(int index) { + if (index < 0 || index > size) { + throw new ArrayListIndexOutOfBoundsException("Index " + index + + " is out of bounds for size " + size); + } + } + + private void ensureCapacity() { + ensureCapacity(size + 1); + } + private void ensureCapacity(int minCapacity) { + if (minCapacity > elementData.length) { + int newLength = elementData.length == 0 ? DEFAULT_CAPACITY + : (int) (elementData.length * 1.5); + if (newLength < minCapacity) { + newLength = minCapacity; + } + Object[] newElementData = new Object[newLength]; + System.arraycopy(elementData,0, newElementData, 0, size); + this.elementData = newElementData; + } } @Override - public void addAll(List list) { + public void add(T value) { + ensureCapacity(); + this.elementData[size] = value; + size++; + } + @Override + public void add(T value, int index) { + checkIndexForAdd(index); + ensureCapacity(); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = value; + size++; } @Override - public T get(int index) { - return null; + public void addAll(List list) { + ensureCapacity(size + list.size()); + for (int i = 0; i < list.size(); i++) { + elementData[size] = list.get(i); + size++; + } } @Override public void set(T value, int index) { + checkIndex(index); + elementData[index] = value; + } + @Override + public T get(int index) { + checkIndex(index); + return (T) elementData[index]; } @Override public T remove(int index) { - return null; + checkIndex(index); + int numMoved = size - index - 1; + T removedValue = (T) elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, numMoved); + elementData[--size] = null; + return removedValue; } @Override public T remove(T element) { - return null; + if (element == null) { + for (int i = 0; i < size; i++) { + if (elementData[i] == null) { + return remove(i); + } + } + } else { + for (int i = 0; i < size; i++) { + if (element.equals(elementData[i])) { + return remove(i); + } + } + } + throw new NoSuchElementException("Element " + element + " not found in the list"); } @Override public int size() { - return 0; + return size; } @Override public boolean isEmpty() { - return false; + return size == 0; } }