From 57f702fe57f399871c2eeef24ddc7688da1e9f61 Mon Sep 17 00:00:00 2001 From: xMaXD Date: Tue, 21 Jan 2025 23:23:33 +0100 Subject: [PATCH 1/3] done --- src/main/java/core/basesyntax/ArrayList.java | 83 +++++++++++++++++--- 1 file changed, 74 insertions(+), 9 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 8d6477943..71d447d95 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,48 +1,113 @@ package core.basesyntax; +import java.util.Arrays; +import java.util.NoSuchElementException; + public class ArrayList implements List { + private Object[] elements; + private int size; + + public ArrayList() { + elements = new Object[10]; + size = 0; + } + @Override public void add(T value) { - + ifFullResize(); + elements[size++] = value; } @Override public void add(T value, int index) { - + validateIndexForAdd(index); + ifFullResize(); + if (index >= 0 && index <= size) { + for (int i = size; i > index; i--) { + elements[i] = elements[i - 1]; + } + elements[index] = value; + size++; + } } @Override public void addAll(List list) { - + for (int i = 0; i < list.size(); i++) { + add(list.get(i)); + } } @Override public T get(int index) { - return null; + validateIndex(index); + return (T) elements[index]; } @Override public void set(T value, int index) { - + validateIndex(index); + elements[index] = value; } @Override public T remove(int index) { - return null; + validateIndex(index); + ifFullResize(); + T removedElement = (T) elements[index]; + for (int i = index; i < size - 1; i++) { + elements[i] = elements[i + 1]; + } + size--; + return removedElement; } @Override public T remove(T element) { - return null; + for (int i = 0; i < size; i++) { + if (elements[i] != null && elements[i].equals(element) + || elements[i] == null && element == null) { + return remove(i); + } + } + throw new NoSuchElementException("Element not found in the list"); } @Override public int size() { - return 0; + return size; } @Override public boolean isEmpty() { - return false; + return size == 0; + } + + private void ifFullResize() { + if (size == elements.length) { + resize(); + } + } + + private void resize() { + elements = Arrays.copyOf(elements, elements.length + (elements.length / 2)); + } + + private void validateIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException("Index " + + index + + " is out of bounds for size " + + size); + } + } + + private void validateIndexForAdd(int index) { + if (index < 0 || index > size) { + throw new ArrayListIndexOutOfBoundsException("Index " + + index + + " is out of bounds for size " + + size); + } } } From 3c11358a7adfd2440b3b18e7ec4019add017ea92 Mon Sep 17 00:00:00 2001 From: xMaXD Date: Tue, 21 Jan 2025 23:30:24 +0100 Subject: [PATCH 2/3] fix1 --- src/main/java/core/basesyntax/ArrayList.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 71d447d95..0f614dd03 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -22,13 +22,11 @@ public void add(T value) { public void add(T value, int index) { validateIndexForAdd(index); ifFullResize(); - if (index >= 0 && index <= size) { - for (int i = size; i > index; i--) { - elements[i] = elements[i - 1]; - } - elements[index] = value; - size++; + for (int i = size; i > index; i--) { + elements[i] = elements[i - 1]; } + elements[index] = value; + size++; } @Override @@ -53,7 +51,6 @@ public void set(T value, int index) { @Override public T remove(int index) { validateIndex(index); - ifFullResize(); T removedElement = (T) elements[index]; for (int i = index; i < size - 1; i++) { elements[i] = elements[i + 1]; From 16cbd084d09781d1ba54a9bb0a0c9350c80a55c6 Mon Sep 17 00:00:00 2001 From: xMaXD Date: Thu, 23 Jan 2025 00:09:41 +0100 Subject: [PATCH 3/3] fix2 --- src/main/java/core/basesyntax/ArrayList.java | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 0f614dd03..b7a3bf51c 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,15 +1,14 @@ package core.basesyntax; -import java.util.Arrays; import java.util.NoSuchElementException; public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 10; private Object[] elements; private int size; public ArrayList() { - elements = new Object[10]; - size = 0; + elements = new Object[DEFAULT_CAPACITY]; } @Override @@ -22,9 +21,7 @@ public void add(T value) { public void add(T value, int index) { validateIndexForAdd(index); ifFullResize(); - for (int i = size; i > index; i--) { - elements[i] = elements[i - 1]; - } + System.arraycopy(elements, index, elements, index + 1, size - index); elements[index] = value; size++; } @@ -52,9 +49,7 @@ public void set(T value, int index) { public T remove(int index) { validateIndex(index); T removedElement = (T) elements[index]; - for (int i = index; i < size - 1; i++) { - elements[i] = elements[i + 1]; - } + System.arraycopy(elements, index + 1, elements, index, size - index - 1); size--; return removedElement; } @@ -63,7 +58,7 @@ public T remove(int index) { public T remove(T element) { for (int i = 0; i < size; i++) { if (elements[i] != null && elements[i].equals(element) - || elements[i] == null && element == null) { + || elements[i] == element) { return remove(i); } } @@ -87,7 +82,10 @@ private void ifFullResize() { } private void resize() { - elements = Arrays.copyOf(elements, elements.length + (elements.length / 2)); + int newCapacity = elements.length * (elements.length / 2); + Object[] newElements = new Object[newCapacity]; + System.arraycopy(elements, 0, newElements, 0, size); + elements = newElements; } private void validateIndex(int index) {