From c0e0686fb21f0824980e27bb5ed65ded5ae18592 Mon Sep 17 00:00:00 2001 From: Yevhenii Liashenko Date: Fri, 27 Oct 2023 18:19:35 +0300 Subject: [PATCH] implement `ArrayList` --- src/main/java/core/basesyntax/ArrayList.java | 79 +++++++++++++++++--- 1 file changed, 70 insertions(+), 9 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 8d6477943..000b66fb9 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,48 +1,109 @@ package core.basesyntax; +import java.util.NoSuchElementException; + public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 10; + private Object[] elementData; + private int size; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + @Override public void add(T value) { - + if (size == elementData.length) { + grow(); + } + elementData[size] = value; + size++; } @Override public void add(T value, int index) { - + if (index < 0 || index > size) { + throw new ArrayListIndexOutOfBoundsException( + "Index " + index + " out of bounds for length" + elementData.length); + } + if (size == elementData.length) { + grow(); + } + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = value; + size++; } @Override public void addAll(List list) { - + while (size + list.size() >= elementData.length) { + grow(); + } + for (int i = 0; i < list.size(); i++) { + elementData[size + i] = list.get(i); + } + size += list.size(); } @Override public T get(int index) { - return null; + checkToCorrectIndex(index); + return elementData(index); } @Override public void set(T value, int index) { - + checkToCorrectIndex(index); + elementData[index] = value; } @Override public T remove(int index) { - return null; + checkToCorrectIndex(index); + final T removedElement = elementData(index); + System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); + elementData[size - 1] = null; + size--; + return removedElement; } @Override public T remove(T element) { - return null; + for (int i = 0; i < size; i++) { + if (elementData[i] == element + || elementData[i] != null && elementData[i].equals(element)) { + remove(i); + return element; + } + } + throw new NoSuchElementException("ArrayList don't have such element"); } @Override public int size() { - return 0; + return size; } @Override public boolean isEmpty() { - return false; + return size == 0; + } + + private T elementData(int index) { + return (T) elementData[index]; + } + + private void checkToCorrectIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException( + "Index " + index + " out of bounds for length" + elementData.length); + } + } + + private void grow() { + int newCapacity = (int)(elementData.length * 1.5f); + Object[] newElementData = new Object[newCapacity]; + System.arraycopy(elementData, 0, newElementData, 0, elementData.length); + elementData = newElementData; } }