From a172c59fdd27e387188f525084a726a0ac35af3f Mon Sep 17 00:00:00 2001 From: ivanlavrinenko Date: Thu, 16 Jan 2025 03:18:49 +0100 Subject: [PATCH 1/2] solution --- src/test/java/core/basesyntax/ArrayList.java | 101 ++++++++++++++++++ .../ArrayListIndexOutOfBoundsException.java | 7 ++ 2 files changed, 108 insertions(+) create mode 100644 src/test/java/core/basesyntax/ArrayList.java create mode 100644 src/test/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java diff --git a/src/test/java/core/basesyntax/ArrayList.java b/src/test/java/core/basesyntax/ArrayList.java new file mode 100644 index 000000000..eb3cd48d0 --- /dev/null +++ b/src/test/java/core/basesyntax/ArrayList.java @@ -0,0 +1,101 @@ +package core.basesyntax; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +public class ArrayList { + private static final int DEFAULT_CAPACITY = 10; + private E[] elements; + private int size; + + public ArrayList() { + elements = (E[]) new Object[DEFAULT_CAPACITY]; + size = 0; + } + + public void add(E value) { + ensureCapacity(); + elements[size++] = value; + } + + public void add(E value, int index) { + validateIndexForAdd(index); + ensureCapacity(); + System.arraycopy(elements, index, elements, index + 1, size - index); + elements[index] = value; + size++; + } + + public void addAll(ArrayList list) { + if (list == null || list.isEmpty()) { + return; + } + + for (int i = 0; i < list.size; i++) { + add(list.get(i)); + } + } + + public E remove(int index) { + validateIndex(index); + E removedElement = elements[index]; + System.arraycopy(elements, index + 1, elements, index, size - index - 1); + elements[--size] = null; + + return removedElement; + } + + public E remove(E value) { + for (int i = 0; i < size; i++) { + if ((value == null && elements[i] == null) || (value != null && value.equals(elements[i]))) { + return remove(i); + } + } + + throw new NoSuchElementException("Element not found: " + value); + } + + public E get(int index) { + validateIndex(index); + + return elements[index]; + } + + public void set(E value, int index) { + validateIndex(index); + elements[index] = value; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + private void ensureCapacity() { + if (size == elements.length) { + elements = Arrays.copyOf(elements, elements.length * 2); + } + } + + private void ensureCapacity(int minCapacity) { + if (minCapacity > elements.length) { + int newCapacity = Math.max(elements.length * 2, minCapacity); + elements = Arrays.copyOf(elements, newCapacity); + } + } + + private void validateIndex(int index) { + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException("Index out of bounds: " + index); + } + } + + private void validateIndexForAdd(int index) { + if (index < 0 || index > size) { + throw new ArrayListIndexOutOfBoundsException("Index out of bounds for add: " + index); + } + } +} diff --git a/src/test/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java b/src/test/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java new file mode 100644 index 000000000..efe1f8132 --- /dev/null +++ b/src/test/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java @@ -0,0 +1,7 @@ +package core.basesyntax; + +public class ArrayListIndexOutOfBoundsException extends IndexOutOfBoundsException { + public ArrayListIndexOutOfBoundsException(String message) { + super(message); + } +} From 7c37ffb30bcc1bd9e926170bd6a6211b38c66155 Mon Sep 17 00:00:00 2001 From: ivanlavrinenko Date: Thu, 16 Jan 2025 03:23:25 +0100 Subject: [PATCH 2/2] solution --- src/test/java/core/basesyntax/ArrayList.java | 8 +------- .../basesyntax/ArrayListIndexOutOfBoundsException.java | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/test/java/core/basesyntax/ArrayList.java b/src/test/java/core/basesyntax/ArrayList.java index eb3cd48d0..c01f7fc76 100644 --- a/src/test/java/core/basesyntax/ArrayList.java +++ b/src/test/java/core/basesyntax/ArrayList.java @@ -1,5 +1,6 @@ package core.basesyntax; +import core.basesyntax.ArrayListIndexOutOfBoundsException; import java.util.Arrays; import java.util.NoSuchElementException; @@ -80,13 +81,6 @@ private void ensureCapacity() { } } - private void ensureCapacity(int minCapacity) { - if (minCapacity > elements.length) { - int newCapacity = Math.max(elements.length * 2, minCapacity); - elements = Arrays.copyOf(elements, newCapacity); - } - } - private void validateIndex(int index) { if (index < 0 || index >= size) { throw new ArrayListIndexOutOfBoundsException("Index out of bounds: " + index); diff --git a/src/test/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java b/src/test/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java index efe1f8132..d39fbe402 100644 --- a/src/test/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java +++ b/src/test/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java @@ -1,6 +1,6 @@ package core.basesyntax; -public class ArrayListIndexOutOfBoundsException extends IndexOutOfBoundsException { +public class ArrayListIndexOutOfBoundsException extends RuntimeException { public ArrayListIndexOutOfBoundsException(String message) { super(message); }