From 4948895c58d71cee2a15724f745afbf470ea58a4 Mon Sep 17 00:00:00 2001 From: ArtShevchenko89 Date: Fri, 15 May 2026 16:44:48 +0200 Subject: [PATCH 1/2] Implement ArrayList with core methods: add, addAll, get, set, remove, size, and isEmpty. Add helper methods checkIndex and growIfArrayFull --- src/main/java/core/basesyntax/ArrayList.java | 96 ++++++++++++++++++-- 1 file changed, 88 insertions(+), 8 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 8d6477943..40cad4bc8 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,48 +1,128 @@ package core.basesyntax; +import java.util.NoSuchElementException; +import java.util.Objects; + public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 10; + private static final double GROWTH_FACTOR = 1.5; + + private T[] data; + private int size; + + public ArrayList() { + this.data = (T[]) new Object[DEFAULT_CAPACITY]; + this.size = 0; + } + + public ArrayList(T[] elementData, int size) { + if (elementData == null) { + throw new IllegalArgumentException("elementData is null"); + } + if (size < 0 || size > elementData.length) { + throw new IllegalArgumentException("Invalid size: " + size); + } + this.data = elementData; + this.size = size; + } + @Override public void add(T value) { - + if (size == data.length) { + growIfArrayFull(); + } + data[size] = value; + size++; } @Override public void add(T value, int index) { + if (index < 0 || index > size) { + throw new ArrayListIndexOutOfBoundsException( + "Index " + index + " is out of bounds for size " + size); + } + if (size == data.length) { + growIfArrayFull(); + } + + System.arraycopy(data, index, data, index + 1, size - index); + data[index] = value; + size++; } @Override public void addAll(List list) { - + if (list == null) { + throw new IllegalArgumentException("list is null"); + } + for (int i = 0; i < list.size(); i++) { + add(list.get(i)); + } } @Override public T get(int index) { - return null; + checkIndex(index); + return data[index]; } @Override public void set(T value, int index) { - + checkIndex(index); + data[index] = value; } @Override public T remove(int index) { - return null; + checkIndex(index); + + final T removed = data[index]; + + int elementsToMove = size - index - 1; + if (elementsToMove > 0) { + System.arraycopy(data, index + 1, data, index, elementsToMove); + } + + data[size - 1] = null; + size--; + + return removed; } @Override public T remove(T element) { - return null; + for (int i = 0; i < size; i++) { + if (Objects.equals(data[i], element)) { + T removed = data[i]; + remove(i); + return removed; + } + } + throw new NoSuchElementException("element not found"); } @Override public int size() { - return 0; + return size; } @Override public boolean isEmpty() { - return false; + return size == 0; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException( + "Index " + index + " is out of bounds for size " + size); + } + } + + private void growIfArrayFull() { + int newCapacity = (int) (data.length * GROWTH_FACTOR); + T[] newData = (T[]) new Object[newCapacity]; + System.arraycopy(data, 0, newData, 0, size); + data = newData; } } From 8808349946ae3a4735f6e9fc30515bbbb0dea04c Mon Sep 17 00:00:00 2001 From: ArtShevchenko89 Date: Fri, 15 May 2026 17:32:30 +0200 Subject: [PATCH 2/2] Fix ArrayList implementation: remove Objects usage and improve null-safe comparison --- src/main/java/core/basesyntax/ArrayList.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 40cad4bc8..62086b32e 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,7 +1,6 @@ package core.basesyntax; import java.util.NoSuchElementException; -import java.util.Objects; public class ArrayList implements List { private static final int DEFAULT_CAPACITY = 10; @@ -93,7 +92,8 @@ public T remove(int index) { @Override public T remove(T element) { for (int i = 0; i < size; i++) { - if (Objects.equals(data[i], element)) { + if ((data[i] == null && element == null) + || (data[i] != null && data[i].equals(element))) { T removed = data[i]; remove(i); return removed;