From 16ee808b9eddfdb1ac7f77b3a3a8d003949146cc Mon Sep 17 00:00:00 2001 From: Andriy Starzhevskiy Date: Fri, 3 Nov 2023 14:37:10 +0200 Subject: [PATCH 1/5] Overridden ArrayList methods --- src/main/java/core/basesyntax/ArrayList.java | 79 +++++++++++++++++--- 1 file changed, 70 insertions(+), 9 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 8d6477943..1a58708cd 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,48 +1,109 @@ package core.basesyntax; +import java.util.NoSuchElementException; + public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 10; + private int size; + private T[] elementData; + + public ArrayList() { + this.elementData = (T[]) new Object[DEFAULT_CAPACITY]; + this.size = 0; + } + + public void indexValidator(int index) { + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException("Index is not valid"); + } + } + + public Object[] grow(T[] elementData, int size) { + if ((size + 1) >= elementData.length) { + int newCapacity = elementData.length + (elementData.length >> 1); + T[] newElementData = (T[]) new Object[newCapacity]; + System.arraycopy(elementData, 0, newElementData, 0, size); + elementData = newElementData; + } + return elementData; + } + @Override public void add(T value) { - + elementData = (T[]) grow(elementData, size); + elementData[size] = value; + size++; } @Override public void add(T value, int index) { - + if (index < 0 || index > size) { + throw new ArrayListIndexOutOfBoundsException("Index is not valid"); + } + if (size == elementData.length) { + elementData = (T[]) grow(elementData, size); + } + for (int i = size; i > index; i--) { + elementData[i] = elementData[i - 1]; + } + elementData[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; + indexValidator(index); + return elementData[index]; } @Override public void set(T value, int index) { + indexValidator(index); + elementData[index] = value; } @Override public T remove(int index) { - return null; + indexValidator(index); + T removedValue = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return removedValue; } @Override public T remove(T element) { - return null; + if (element == null) { + for (int i = 0; i < size; i++) { + if (elementData[i] == null) { + return remove(i); + } + } + } else { + for (int i = 0; i < size; i++) { + if (element.equals(elementData[i])) { + return remove(i); + } + } + } + throw new NoSuchElementException("Element not found"); } @Override public int size() { - return 0; + return size; } @Override public boolean isEmpty() { - return false; + return size == 0; } -} +} \ No newline at end of file From e41a0d00bddc309db8ea6974e09da7d17ef62d25 Mon Sep 17 00:00:00 2001 From: Andriy Starzhevskiy Date: Fri, 3 Nov 2023 14:38:16 +0200 Subject: [PATCH 2/5] checkstyle fixed --- 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 1a58708cd..0472e7006 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -106,4 +106,4 @@ public int size() { public boolean isEmpty() { return size == 0; } -} \ No newline at end of file +} From 891984418601b0efb49940d14918eb31c40bad76 Mon Sep 17 00:00:00 2001 From: Andriy Starzhevskiy Date: Fri, 3 Nov 2023 18:47:45 +0200 Subject: [PATCH 3/5] the grow method is now private void created addIndexValidator method --- src/main/java/core/basesyntax/ArrayList.java | 47 +++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 0472e7006..fe2508e35 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -9,39 +9,20 @@ public class ArrayList implements List { public ArrayList() { this.elementData = (T[]) new Object[DEFAULT_CAPACITY]; - this.size = 0; - } - - public void indexValidator(int index) { - if (index < 0 || index >= size) { - throw new ArrayListIndexOutOfBoundsException("Index is not valid"); - } - } - - public Object[] grow(T[] elementData, int size) { - if ((size + 1) >= elementData.length) { - int newCapacity = elementData.length + (elementData.length >> 1); - T[] newElementData = (T[]) new Object[newCapacity]; - System.arraycopy(elementData, 0, newElementData, 0, size); - elementData = newElementData; - } - return elementData; } @Override public void add(T value) { - elementData = (T[]) grow(elementData, size); + grow(elementData, size); elementData[size] = value; size++; } @Override public void add(T value, int index) { - if (index < 0 || index > size) { - throw new ArrayListIndexOutOfBoundsException("Index is not valid"); - } + addIndexValidator(index); if (size == elementData.length) { - elementData = (T[]) grow(elementData, size); + grow(elementData, size); } for (int i = size; i > index; i--) { elementData[i] = elementData[i - 1]; @@ -106,4 +87,26 @@ public int size() { public boolean isEmpty() { return size == 0; } + + + private void indexValidator(int index) { + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException("Index is not valid"); + } + } + + private void addIndexValidator(int index) { + if (index < 0 || index > size) { + throw new ArrayListIndexOutOfBoundsException("Index is not valid"); + } + } + + private void grow(T[] elementData, int size) { + if ((size + 1) >= elementData.length) { + int newCapacity = elementData.length + (elementData.length >> 1); + T[] newElementData = (T[]) new Object[newCapacity]; + System.arraycopy(elementData, 0, newElementData, 0, size); + this.elementData = newElementData; + } + } } From 973b67525464506a26d5fa2c49222ebc845a063b Mon Sep 17 00:00:00 2001 From: Andriy Starzhevskiy Date: Fri, 3 Nov 2023 18:48:30 +0200 Subject: [PATCH 4/5] the grow method is now private void created addIndexValidator method --- src/main/java/core/basesyntax/ArrayList.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index fe2508e35..21b08b557 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -88,7 +88,6 @@ public boolean isEmpty() { return size == 0; } - private void indexValidator(int index) { if (index < 0 || index >= size) { throw new ArrayListIndexOutOfBoundsException("Index is not valid"); From 9a96c927b2c4fcc70e729bd93f00915aa4adfecf Mon Sep 17 00:00:00 2001 From: Andriy Starzhevskiy Date: Fri, 3 Nov 2023 20:18:58 +0200 Subject: [PATCH 5/5] corrected mistakes --- src/main/java/core/basesyntax/ArrayList.java | 21 ++++++-------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 21b08b557..36423d000 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -4,6 +4,7 @@ public class ArrayList implements List { private static final int DEFAULT_CAPACITY = 10; + private static final double GROWTH_FACTOR = 1.5; private int size; private T[] elementData; @@ -24,9 +25,7 @@ public void add(T value, int index) { if (size == elementData.length) { grow(elementData, size); } - for (int i = size; i > index; i--) { - elementData[i] = elementData[i - 1]; - } + System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = value; size++; } @@ -62,17 +61,9 @@ public T remove(int index) { @Override public T remove(T element) { - if (element == null) { - for (int i = 0; i < size; i++) { - if (elementData[i] == null) { - return remove(i); - } - } - } else { - for (int i = 0; i < size; i++) { - if (element.equals(elementData[i])) { - return remove(i); - } + for (int i = 0; i < size; i++) { + if (element == null ? elementData[i] == null : element.equals(elementData[i])) { + return remove(i); } } throw new NoSuchElementException("Element not found"); @@ -102,7 +93,7 @@ private void addIndexValidator(int index) { private void grow(T[] elementData, int size) { if ((size + 1) >= elementData.length) { - int newCapacity = elementData.length + (elementData.length >> 1); + int newCapacity = (int) (elementData.length * GROWTH_FACTOR); T[] newElementData = (T[]) new Object[newCapacity]; System.arraycopy(elementData, 0, newElementData, 0, size); this.elementData = newElementData;