diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 4bf0c8598..9920b6a8e 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -4,40 +4,27 @@ import java.util.NoSuchElementException; public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 10; private T[] elements; private int size; - private static final int DEFAULT_CAPACITY = 10; - private static final int START_SIZE = 0; public ArrayList() { elements = (T[]) new Object[DEFAULT_CAPACITY]; - size = START_SIZE; + size = 0; } @Override public void add(T value) { - if (size == elements.length) { - int newCapacity = (int) (elements.length * 1.5); - elements = Arrays.copyOf(elements, newCapacity); - } + growIfNeeded(); elements[size] = value; size++; } @Override public void add(T value, int index) { - if (index < 0 || index > size) { - throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index); - } - - if (size == elements.length) { - int newCapacity = (int) (elements.length * 1.5); - elements = Arrays.copyOf(elements, newCapacity); - } - - for (int i = size; i > index; i--) { - elements[i] = elements[i - 1]; - } + checkIndexForAdd(index); + growIfNeeded(); + shiftRight(index); elements[index] = value; size++; } @@ -47,50 +34,38 @@ public void addAll(List list) { if (list == null) { throw new NullPointerException("The provided list is null"); } - + ensureCapacity(size + list.size()); for (int i = 0; i < list.size(); i++) { - this.add(list.get(i)); + add(list.get(i)); } } @Override public T get(int index) { - if (index < 0 || index >= size) { - throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index); - } + checkIndex(index); return elements[index]; } @Override public void set(T value, int index) { - if (index < 0 || index >= size) { - throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index); - } + checkIndex(index); elements[index] = value; } @Override public T remove(int index) { - if (index < 0 || index >= size) { - throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index); - } - + checkIndex(index); T removedElement = elements[index]; - - for (int i = index; i < size - 1; i++) { - elements[i] = elements[i + 1]; - } - - elements[size - 1] = null; + shiftLeft(index); size--; - return removedElement; } @Override public T remove(T element) { for (int i = 0; i < size; i++) { - if (elements[i].equals(element)) { + if ((elements[i] == null && element == null) || (elements[i] + != null && elements[i].equals(element))) { return remove(i); } } @@ -106,4 +81,48 @@ public int size() { public boolean isEmpty() { return size == 0; } + + private void growIfNeeded() { + if (size == elements.length) { + int newCapacity = (int) (elements.length * 1.5); + elements = Arrays.copyOf(elements, newCapacity); + } + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity > elements.length) { + int newCapacity = elements.length; + while (newCapacity < minCapacity) { + newCapacity = (int) (newCapacity * 1.5); + } + elements = Arrays.copyOf(elements, newCapacity); + } + } + + private void shiftRight(int index) { + for (int i = size; i > index; i--) { + elements[i] = elements[i - 1]; + } + } + + private void shiftLeft(int index) { + for (int i = index; i < size - 1; i++) { + elements[i] = elements[i + 1]; + } + elements[size - 1] = null; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException("Index " + index + + " is out of bounds. Size: " + size); + } + } + + private void checkIndexForAdd(int index) { + if (index < 0 || index > size) { + throw new ArrayListIndexOutOfBoundsException("Index " + index + + " is out of bounds for add. Size: " + size); + } + } }