From 309c0ce379e01c32915c53a030abfd8b7ede3cac Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 00:30:31 +0200 Subject: [PATCH 01/17] task jv-array-list is done --- src/main/java/core/basesyntax/ArrayList.java | 81 ++++++++++++++++++-- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 8d6477943..feb137c6d 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,14 +1,62 @@ package core.basesyntax; +import java.util.Arrays; +import java.util.NoSuchElementException; + public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 2; + private int size; + private Object[] elementData; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + + public void rangeCheck() { + if (size == elementData.length) { + grow(); + } + } + + public void indexCheck(int index) { + if (index >= size || index < 0) { + throw new ArrayListIndexOutOfBoundsException("Invalid index"); + } + } + + private Object[] grow() { + int newCapacity = elementData.length * 3 / 2 + 1; + elementData = Arrays.copyOf(elementData, newCapacity); + return elementData; + } + + public void checkIfElementExists(T element) { + boolean elementFaunded = false; + for (int i = 0; i < size; i++) { + if ((element == null && elementData[i] == null) + || (element != null && element.equals(elementData[i]))) { + elementFaunded = true; + break; + } + } + if (!elementFaunded) { + throw new NoSuchElementException("Element not found in the array"); + } + } + @Override public void add(T value) { - + rangeCheck(); + elementData[size] = value; + size++; } @Override public void add(T value, int index) { - + if (index > size || index < 0) { + throw new ArrayListIndexOutOfBoundsException("Index out of bound ... )"); + } + rangeCheck(); } @Override @@ -18,9 +66,18 @@ public void addAll(List list) { @Override public T get(int index) { - return null; + indexCheck(index); + checkIfElementExistsByIndex(index); + return (T) elementData[index]; } + private void checkIfElementExistsByIndex(int index) { + if (elementData[index] == null) { + throw new RuntimeException("There is no element by this index. Please call to admin to support"); + } + } + + @Override public void set(T value, int index) { @@ -28,21 +85,33 @@ public void set(T value, int index) { @Override public T remove(int index) { - return null; + indexCheck(index); + final T removedElement = (T) elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + elementData[size - 1] = null; + size--; + return removedElement; } @Override public T remove(T element) { + checkIfElementExists(element); + for (int i = 0; i < size; i++) { + if ((element == null && elementData[i] == null) || (element != null && element.equals(elementData[i]))) { + T removedElement = remove(i); + return removedElement; + } + } return null; } @Override public int size() { - return 0; + return size; } @Override public boolean isEmpty() { - return false; + return size == 0; } } From a69e48f442ed369e8de5bc4edee3477745e36550 Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 01:39:57 +0200 Subject: [PATCH 02/17] issues is done --- src/main/java/core/basesyntax/ArrayList.java | 39 ++++---------------- src/main/java/core/basesyntax/List.java | 2 - 2 files changed, 7 insertions(+), 34 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index feb137c6d..12f802ea6 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,7 +1,7 @@ package core.basesyntax; + import java.util.Arrays; -import java.util.NoSuchElementException; public class ArrayList implements List { private static final int DEFAULT_CAPACITY = 2; @@ -30,20 +30,6 @@ private Object[] grow() { return elementData; } - public void checkIfElementExists(T element) { - boolean elementFaunded = false; - for (int i = 0; i < size; i++) { - if ((element == null && elementData[i] == null) - || (element != null && element.equals(elementData[i]))) { - elementFaunded = true; - break; - } - } - if (!elementFaunded) { - throw new NoSuchElementException("Element not found in the array"); - } - } - @Override public void add(T value) { rangeCheck(); @@ -53,34 +39,23 @@ public void add(T value) { @Override public void add(T value, int index) { - if (index > size || index < 0) { - throw new ArrayListIndexOutOfBoundsException("Index out of bound ... )"); - } rangeCheck(); } - @Override - public void addAll(List list) { - - } - @Override public T get(int index) { indexCheck(index); - checkIfElementExistsByIndex(index); return (T) elementData[index]; } - private void checkIfElementExistsByIndex(int index) { - if (elementData[index] == null) { - throw new RuntimeException("There is no element by this index. Please call to admin to support"); - } - } - - @Override public void set(T value, int index) { + for (int i = 0; i < elementData.length; i++) { + if (i == index) { + elementData[i] = value; + } + } } @Override @@ -95,7 +70,7 @@ public T remove(int index) { @Override public T remove(T element) { - checkIfElementExists(element); + rangeCheck(); for (int i = 0; i < size; i++) { if ((element == null && elementData[i] == null) || (element != null && element.equals(elementData[i]))) { T removedElement = remove(i); diff --git a/src/main/java/core/basesyntax/List.java b/src/main/java/core/basesyntax/List.java index f36f849a7..d21f2a758 100644 --- a/src/main/java/core/basesyntax/List.java +++ b/src/main/java/core/basesyntax/List.java @@ -5,8 +5,6 @@ public interface List { void add(T value, int index); - void addAll(List list); - T get(int index); void set(T value, int index); From 70f6ec877be64b23b32c07f537bc386d72720522 Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 02:13:36 +0200 Subject: [PATCH 03/17] issues is done --- src/main/java/core/basesyntax/ArrayList.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 12f802ea6..32b9438ac 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,10 +1,7 @@ package core.basesyntax; - -import java.util.Arrays; - public class ArrayList implements List { - private static final int DEFAULT_CAPACITY = 2; + private static final int DEFAULT_CAPACITY = 5; private int size; private Object[] elementData; @@ -26,7 +23,7 @@ public void indexCheck(int index) { private Object[] grow() { int newCapacity = elementData.length * 3 / 2 + 1; - elementData = Arrays.copyOf(elementData, newCapacity); + elementData = new Object[newCapacity]; return elementData; } From 99ef1eae57ed78930fd6239334e5914d5f011e1e Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 02:14:54 +0200 Subject: [PATCH 04/17] issues is done --- src/main/java/core/basesyntax/ArrayList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 32b9438ac..093b27cf8 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,7 +1,7 @@ package core.basesyntax; public class ArrayList implements List { - private static final int DEFAULT_CAPACITY = 5; + private static final int DEFAULT_CAPACITY = 10K; private int size; private Object[] elementData; From db65f51eccdc41435fc339222da0543dc3c62e5d Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 02:16:10 +0200 Subject: [PATCH 05/17] issues is done --- src/main/java/core/basesyntax/ArrayList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 093b27cf8..c6d95595f 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,7 +1,7 @@ package core.basesyntax; public class ArrayList implements List { - private static final int DEFAULT_CAPACITY = 10K; + private static final int DEFAULT_CAPACITY = 10; private int size; private Object[] elementData; From b30829c08e3da47e4b91ff44a11d1d913e13cc09 Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 02:21:00 +0200 Subject: [PATCH 06/17] issues is done --- src/main/java/core/basesyntax/ArrayList.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index c6d95595f..f37e271a4 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -86,4 +86,5 @@ public int size() { public boolean isEmpty() { return size == 0; } + } From 7549d5635ca1befa3929b29c1f10dcdd084401ee Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 02:23:53 +0200 Subject: [PATCH 07/17] issues is done --- src/main/java/core/basesyntax/ArrayList.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index f37e271a4..0dbf898f4 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -69,7 +69,8 @@ public T remove(int index) { public T remove(T element) { rangeCheck(); for (int i = 0; i < size; i++) { - if ((element == null && elementData[i] == null) || (element != null && element.equals(elementData[i]))) { + if ((element == null && elementData[i] == null) || + (element != null && element.equals(elementData[i]))) { T removedElement = remove(i); return removedElement; } From 32fb87a7edffccf96039873a12f765239884ec0f Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 02:33:11 +0200 Subject: [PATCH 08/17] the issues with style-guides is fixed --- pom.xml | 6 +++--- src/main/java/core/basesyntax/ArrayList.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index de9aea41e..a23e1c6df 100644 --- a/pom.xml +++ b/pom.xml @@ -12,9 +12,9 @@ 17 UTF-8 UTF-8 - - https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml - + + + diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 0dbf898f4..1e2269004 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -69,8 +69,8 @@ public T remove(int index) { public T remove(T element) { rangeCheck(); for (int i = 0; i < size; i++) { - if ((element == null && elementData[i] == null) || - (element != null && element.equals(elementData[i]))) { + if ((element == null && elementData[i] == null) + || (element != null && element.equals(elementData[i]))) { T removedElement = remove(i); return removedElement; } From 7f016cc3c5c54e3eccc039579bcf2f32e97fe167 Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 02:47:42 +0200 Subject: [PATCH 09/17] the issues with style-guides is fixed --- pom.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index a23e1c6df..e0e83a06a 100644 --- a/pom.xml +++ b/pom.xml @@ -40,10 +40,10 @@ - ${maven.checkstyle.plugin.configLocation} - true - true - false + + + + From 6f45a5bf04b76d461329856b91f88e50102bd8bf Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 02:57:15 +0200 Subject: [PATCH 10/17] the issues with final access is fixed --- 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 1e2269004..93bb4161a 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -9,13 +9,13 @@ public ArrayList() { elementData = new Object[DEFAULT_CAPACITY]; } - public void rangeCheck() { + private void rangeCheck() { if (size == elementData.length) { grow(); } } - public void indexCheck(int index) { + private void indexCheck(int index) { if (index >= size || index < 0) { throw new ArrayListIndexOutOfBoundsException("Invalid index"); } From fd4d6d9c4700a8615a7015ab2601eb4751bd8b45 Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 03:04:28 +0200 Subject: [PATCH 11/17] the issues with final access is fixed --- src/main/java/core/basesyntax/ArrayList.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 93bb4161a..d165423fb 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -15,7 +15,7 @@ private void rangeCheck() { } } - private void indexCheck(int index) { + private final void indexCheck(final int index) { if (index >= size || index < 0) { throw new ArrayListIndexOutOfBoundsException("Invalid index"); } @@ -28,25 +28,25 @@ private Object[] grow() { } @Override - public void add(T value) { + public final void add(final T value) { rangeCheck(); elementData[size] = value; size++; } @Override - public void add(T value, int index) { + public final void add(final T value, final int index) { rangeCheck(); } @Override - public T get(int index) { + public T get(final int index) { indexCheck(index); return (T) elementData[index]; } @Override - public void set(T value, int index) { + public void set(final T value, final int index) { for (int i = 0; i < elementData.length; i++) { if (i == index) { @@ -56,7 +56,7 @@ public void set(T value, int index) { } @Override - public T remove(int index) { + public T remove(final int index) { indexCheck(index); final T removedElement = (T) elementData[index]; System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); @@ -66,7 +66,7 @@ public T remove(int index) { } @Override - public T remove(T element) { + public T remove(final T element) { rangeCheck(); for (int i = 0; i < size; i++) { if ((element == null && elementData[i] == null) From e4006df3ab0c8ac66f439c5e7ed642144472c5eb Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 03:12:21 +0200 Subject: [PATCH 12/17] the issues with final access is fixed --- src/main/java/core/basesyntax/ArrayList.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index d165423fb..701694ebe 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,6 +1,6 @@ package core.basesyntax; -public class ArrayList implements List { +public final class ArrayList implements List { private static final int DEFAULT_CAPACITY = 10; private int size; private Object[] elementData; @@ -15,7 +15,7 @@ private void rangeCheck() { } } - private final void indexCheck(final int index) { + private void indexCheck(final int index) { if (index >= size || index < 0) { throw new ArrayListIndexOutOfBoundsException("Invalid index"); } @@ -28,14 +28,14 @@ private Object[] grow() { } @Override - public final void add(final T value) { + public void add(final T value) { rangeCheck(); elementData[size] = value; size++; } @Override - public final void add(final T value, final int index) { + public void add(final T value, final int index) { rangeCheck(); } @@ -59,7 +59,8 @@ public void set(final T value, final int index) { public T remove(final int index) { indexCheck(index); final T removedElement = (T) elementData[index]; - System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + System.arraycopy(elementData, index + 1, + elementData, index, size - index - 1); elementData[size - 1] = null; size--; return removedElement; From ebd9cbafb903f5ed5057ad332206e878ce4c0a36 Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 03:18:48 +0200 Subject: [PATCH 13/17] the issues with MAGIC_NUMBER is fixed --- src/main/java/core/basesyntax/ArrayList.java | 3 ++- .../core/basesyntax/ArrayListIndexOutOfBoundsException.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 701694ebe..15fe0fbab 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -2,6 +2,7 @@ public final class ArrayList implements List { private static final int DEFAULT_CAPACITY = 10; + public static final int MAGIC_NUMBER = 3; private int size; private Object[] elementData; @@ -22,7 +23,7 @@ private void indexCheck(final int index) { } private Object[] grow() { - int newCapacity = elementData.length * 3 / 2 + 1; + int newCapacity = elementData.length * MAGIC_NUMBER / 2 + 1; elementData = new Object[newCapacity]; return elementData; } diff --git a/src/main/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java b/src/main/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java index d39fbe402..ccca2fa93 100644 --- a/src/main/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java +++ b/src/main/java/core/basesyntax/ArrayListIndexOutOfBoundsException.java @@ -1,7 +1,7 @@ package core.basesyntax; public class ArrayListIndexOutOfBoundsException extends RuntimeException { - public ArrayListIndexOutOfBoundsException(String message) { + public ArrayListIndexOutOfBoundsException(final String message) { super(message); } } From 5a87560d34e2a4356a511208e6f11d42ce0c87f5 Mon Sep 17 00:00:00 2001 From: mr-green Date: Fri, 10 Jan 2025 03:21:35 +0200 Subject: [PATCH 14/17] the issues maven style-guides is fixed --- pom.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index e0e83a06a..e14151fc1 100644 --- a/pom.xml +++ b/pom.xml @@ -12,9 +12,9 @@ 17 UTF-8 UTF-8 - - - + + https://raw.githubusercontent.com/mate-academy/style-guides/master/java/checkstyle.xml + From b2564e8b4de875eb91d40b508333f986833d2a36 Mon Sep 17 00:00:00 2001 From: mr-green Date: Sun, 12 Jan 2025 13:25:36 +0200 Subject: [PATCH 15/17] the issues maven style-guides is fixed --- pom.xml | 8 ++++---- src/main/java/core/basesyntax/ArrayList.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index e14151fc1..de9aea41e 100644 --- a/pom.xml +++ b/pom.xml @@ -40,10 +40,10 @@ - - - - + ${maven.checkstyle.plugin.configLocation} + true + true + false diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 15fe0fbab..7df40a4f2 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -2,7 +2,7 @@ public final class ArrayList implements List { private static final int DEFAULT_CAPACITY = 10; - public static final int MAGIC_NUMBER = 3; + private static final int MAGIC_NUMBER = 3; private int size; private Object[] elementData; From dae8d5dc62c072f6ef58e4b4ff8377c7f6a9990a Mon Sep 17 00:00:00 2001 From: mr-green Date: Mon, 13 Jan 2025 14:16:07 +0200 Subject: [PATCH 16/17] method addAll() is added --- src/main/java/core/basesyntax/ArrayList.java | 66 ++++++++++++-------- src/main/java/core/basesyntax/List.java | 2 + 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 7df40a4f2..201bbf6bc 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,57 +1,71 @@ package core.basesyntax; +import java.util.Arrays; + public final class ArrayList implements List { private static final int DEFAULT_CAPACITY = 10; private static final int MAGIC_NUMBER = 3; private int size; - private Object[] elementData; + private Object[] elements; public ArrayList() { - elementData = new Object[DEFAULT_CAPACITY]; + elements = new Object[DEFAULT_CAPACITY]; + size = 0; } - private void rangeCheck() { - if (size == elementData.length) { + private void ensureCapacity(int size) { + if (size == elements.length) { grow(); } } private void indexCheck(final int index) { if (index >= size || index < 0) { - throw new ArrayListIndexOutOfBoundsException("Invalid index"); + throw new ArrayListIndexOutOfBoundsException("Index " + index + + " is out of bounds for add operation"); } } - private Object[] grow() { - int newCapacity = elementData.length * MAGIC_NUMBER / 2 + 1; - elementData = new Object[newCapacity]; - return elementData; + private void grow() { + int newCapacity = elements.length * MAGIC_NUMBER / 2 + 1; + elements = Arrays.copyOf(elements, newCapacity); } @Override - public void add(final T value) { - rangeCheck(); - elementData[size] = value; - size++; + public void add(final T element) { + ensureCapacity(size); + elements[size++] = element; } @Override public void add(final T value, final int index) { - rangeCheck(); + indexCheck(index); + 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++) { + this.add(list.get(i)); + } } @Override public T get(final int index) { indexCheck(index); - return (T) elementData[index]; + return (T) elements[index]; } @Override public void set(final T value, final int index) { - for (int i = 0; i < elementData.length; i++) { + for (int i = 0; i < elements.length; i++) { if (i == index) { - elementData[i] = value; + elements[i] = value; } } } @@ -59,22 +73,21 @@ public void set(final T value, final int index) { @Override public T remove(final int index) { indexCheck(index); - final T removedElement = (T) elementData[index]; - System.arraycopy(elementData, index + 1, - elementData, index, size - index - 1); - elementData[size - 1] = null; + final T removedElement = (T) elements[index]; + System.arraycopy(elements, index + 1, + elements, index, size - index - 1); + elements[size - 1] = null; size--; return removedElement; } @Override public T remove(final T element) { - rangeCheck(); + ensureCapacity(size); for (int i = 0; i < size; i++) { - if ((element == null && elementData[i] == null) - || (element != null && element.equals(elementData[i]))) { - T removedElement = remove(i); - return removedElement; + if ((element == null && elements[i] == null) + || (element != null && element.equals(elements[i]))) { + return remove(i); } } return null; @@ -89,5 +102,4 @@ public int size() { public boolean isEmpty() { return size == 0; } - } diff --git a/src/main/java/core/basesyntax/List.java b/src/main/java/core/basesyntax/List.java index d21f2a758..f36f849a7 100644 --- a/src/main/java/core/basesyntax/List.java +++ b/src/main/java/core/basesyntax/List.java @@ -5,6 +5,8 @@ public interface List { void add(T value, int index); + void addAll(List list); + T get(int index); void set(T value, int index); From 7c6c06cfee73427f183e404edded4bf814b39b3e Mon Sep 17 00:00:00 2001 From: mr-green Date: Mon, 13 Jan 2025 16:14:00 +0200 Subject: [PATCH 17/17] issue with ArrayListIndexOutOfBoundsException is fixed --- src/main/java/core/basesyntax/ArrayList.java | 21 ++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 201bbf6bc..cfadff78e 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,6 +1,7 @@ package core.basesyntax; import java.util.Arrays; +import java.util.NoSuchElementException; public final class ArrayList implements List { private static final int DEFAULT_CAPACITY = 10; @@ -20,7 +21,13 @@ private void ensureCapacity(int size) { } private void indexCheck(final int index) { - if (index >= size || index < 0) { + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException("Index " + index + " is out of bounds"); + } + } + + private void checkIndexForAdd(int index) { + if (index < 0 || index > size) { throw new ArrayListIndexOutOfBoundsException("Index " + index + " is out of bounds for add operation"); } @@ -39,10 +46,9 @@ public void add(final T element) { @Override public void add(final T value, final int index) { - indexCheck(index); - for (int i = size; i > index; i--) { - elements[i] = elements[i - 1]; - } + checkIndexForAdd(index); + ensureCapacity(size); + System.arraycopy(elements, index, elements, index + 1, size - index); elements[index] = value; size++; } @@ -62,7 +68,7 @@ public T get(final int index) { @Override public void set(final T value, final int index) { - + indexCheck(index); for (int i = 0; i < elements.length; i++) { if (i == index) { elements[i] = value; @@ -83,14 +89,13 @@ public T remove(final int index) { @Override public T remove(final T element) { - ensureCapacity(size); for (int i = 0; i < size; i++) { if ((element == null && elements[i] == null) || (element != null && element.equals(elements[i]))) { return remove(i); } } - return null; + throw new NoSuchElementException("Element doesn't exist"); } @Override