From ee37d290d39e5601294d230fad197fb9ae17a342 Mon Sep 17 00:00:00 2001 From: Taras Markov Date: Thu, 12 Dec 2024 09:02:25 -0500 Subject: [PATCH 1/4] Relised list --- src/main/java/core/basesyntax/ArrayList.java | 74 ++++++++++++++++---- 1 file changed, 61 insertions(+), 13 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 8d6477943..29e0db877 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,48 +1,96 @@ package core.basesyntax; +import java.util.NoSuchElementException; +import java.util.Objects; + public class ArrayList implements List { + private Object[] array = new Object[10]; + private int size = 1; + @Override public void add(T value) { - + if (size == array.length) { + Object[] newArray = new Object[array.length + array.length / 2]; + System.arraycopy(array, 0, newArray, 0, array.length); + array = newArray; + } + array[size - 1] = value; + size++; } @Override - public void add(T value, int index) { - + public void add(T value, int index) throws ArrayListIndexOutOfBoundsException { + if (index > size() || index < 0) { + throw new ArrayListIndexOutOfBoundsException("Index out of bounds"); + } + if (size >= array.length) { + Object[] newArray = new Object[array.length + array.length / 2]; + System.arraycopy(array, 0, newArray, 0, array.length); + array = newArray; + } + System.arraycopy(array, index, array, index + 1, size() - index); + array[index] = value; + size++; } @Override public void addAll(List list) { - + int summarySize = list.size() + size(); + final int tempSize = summarySize; + summarySize = summarySize + summarySize / 2; + Object[] newArray = new Object[summarySize]; + System.arraycopy(array, 0, newArray, 0, array.length); + array = newArray; + for (int i = 0; i < list.size(); i++) { + array[i + size - 1] = list.get(i); + } + size = tempSize + 1; } @Override - public T get(int index) { - return null; + public T get(int index) throws ArrayListIndexOutOfBoundsException { + if (index >= size - 1 || index < 0) { + throw new ArrayListIndexOutOfBoundsException("Index out of bounds"); + } + return (T) array[index]; } @Override - public void set(T value, int index) { - + public void set(T value, int index) throws ArrayListIndexOutOfBoundsException { + if (index >= size - 1 || index < 0) { + throw new ArrayListIndexOutOfBoundsException("Index out of bounds"); + } + array[index] = value; } @Override public T remove(int index) { - return null; + if (index >= size() || index < 0) { + throw new ArrayListIndexOutOfBoundsException("Index out of bounds"); + } + T result = (T) array[index]; + System.arraycopy(array, index + 1, array, index, array.length - index - 1); + size--; + return result; } @Override - public T remove(T element) { - return null; + public T remove(T element) throws NoSuchElementException { + for (int i = 0; i < size(); i++) { + if (Objects.equals(get(i), element)) { + return remove(i); + } + } + throw new NoSuchElementException(); } @Override public int size() { - return 0; + return size - 1; } @Override public boolean isEmpty() { - return false; + return size() == 0; } } From ffcd5f57f3da92573528873d07ba6cc44209f0e2 Mon Sep 17 00:00:00 2001 From: Taras Markov Date: Thu, 12 Dec 2024 09:10:24 -0500 Subject: [PATCH 2/4] Change size list --- src/main/java/core/basesyntax/ArrayList.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 29e0db877..2263b423f 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -5,16 +5,16 @@ public class ArrayList implements List { private Object[] array = new Object[10]; - private int size = 1; + private int size = 0; @Override public void add(T value) { - if (size == array.length) { + if (size + 1 == array.length) { Object[] newArray = new Object[array.length + array.length / 2]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; } - array[size - 1] = value; + array[size] = value; size++; } @@ -23,7 +23,7 @@ public void add(T value, int index) throws ArrayListIndexOutOfBoundsException { if (index > size() || index < 0) { throw new ArrayListIndexOutOfBoundsException("Index out of bounds"); } - if (size >= array.length) { + if (size + 1 >= array.length) { Object[] newArray = new Object[array.length + array.length / 2]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; @@ -42,14 +42,14 @@ public void addAll(List list) { System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; for (int i = 0; i < list.size(); i++) { - array[i + size - 1] = list.get(i); + array[i + size] = list.get(i); } - size = tempSize + 1; + size = tempSize; } @Override public T get(int index) throws ArrayListIndexOutOfBoundsException { - if (index >= size - 1 || index < 0) { + if (index >= size() || index < 0) { throw new ArrayListIndexOutOfBoundsException("Index out of bounds"); } return (T) array[index]; @@ -57,7 +57,7 @@ public T get(int index) throws ArrayListIndexOutOfBoundsException { @Override public void set(T value, int index) throws ArrayListIndexOutOfBoundsException { - if (index >= size - 1 || index < 0) { + if (index >= size() || index < 0) { throw new ArrayListIndexOutOfBoundsException("Index out of bounds"); } array[index] = value; @@ -86,7 +86,7 @@ public T remove(T element) throws NoSuchElementException { @Override public int size() { - return size - 1; + return size; } @Override From 4761b825971bc867047b04e32371624a208b5ba3 Mon Sep 17 00:00:00 2001 From: Taras Markov Date: Thu, 12 Dec 2024 09:12:55 -0500 Subject: [PATCH 3/4] Change size list --- 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 2263b423f..808a12f37 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -4,8 +4,8 @@ import java.util.Objects; public class ArrayList implements List { - private Object[] array = new Object[10]; private int size = 0; + private Object[] array = new Object[10]; @Override public void add(T value) { From 167f3b3da1b60ec97b50e36f5ad9682f830f1673 Mon Sep 17 00:00:00 2001 From: Taras Markov Date: Thu, 12 Dec 2024 09:26:50 -0500 Subject: [PATCH 4/4] Change size list --- src/main/java/core/basesyntax/ArrayList.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 808a12f37..8c383cc07 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -9,7 +9,7 @@ public class ArrayList implements List { @Override public void add(T value) { - if (size + 1 == array.length) { + if (size == array.length) { Object[] newArray = new Object[array.length + array.length / 2]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; @@ -23,7 +23,7 @@ public void add(T value, int index) throws ArrayListIndexOutOfBoundsException { if (index > size() || index < 0) { throw new ArrayListIndexOutOfBoundsException("Index out of bounds"); } - if (size + 1 >= array.length) { + if (size == array.length) { Object[] newArray = new Object[array.length + array.length / 2]; System.arraycopy(array, 0, newArray, 0, array.length); array = newArray; @@ -37,10 +37,12 @@ public void add(T value, int index) throws ArrayListIndexOutOfBoundsException { public void addAll(List list) { int summarySize = list.size() + size(); final int tempSize = summarySize; - summarySize = summarySize + summarySize / 2; - Object[] newArray = new Object[summarySize]; - System.arraycopy(array, 0, newArray, 0, array.length); - array = newArray; + if (summarySize > array.length) { + summarySize = summarySize + summarySize / 2; + Object[] newArray = new Object[summarySize]; + System.arraycopy(array, 0, newArray, 0, array.length); + array = newArray; + } for (int i = 0; i < list.size(); i++) { array[i + size] = list.get(i); }