From 1006cdcb982fefb15a1e2326b002ae84b9429c1c Mon Sep 17 00:00:00 2001 From: Evgen Date: Sun, 24 May 2026 14:35:45 +0300 Subject: [PATCH 1/2] Implemented methods for ArrayList class --- src/main/java/core/basesyntax/ArrayList.java | 88 ++++++++++++++++++-- 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 8d6477943..56239e46b 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,48 +1,120 @@ package core.basesyntax; +import java.util.Arrays; +import java.util.NoSuchElementException; + public class ArrayList implements List { + private Object[] elementData = new Object[10]; + private int size; + @Override public void add(T value) { - + elementData = getNewCapacityArray(elementData); + elementData[size()] = value; + size++; } @Override public void add(T value, int index) { - + if (index > size() || index < 0) { + throw new ArrayListIndexOutOfBoundsException("This index doesn't exist"); + } + elementData = getNewCapacityArray(elementData); + System.arraycopy(elementData, index, elementData, index + 1, size() - index); + elementData[index] = value; + size++; } @Override public void addAll(List list) { - + if (list.size() == 0) { + return; + } + Object[] arrayFromCollection = getArrayFromCollection(list); + int capacity = list.size() + size(); + Object[] summaryArray = new Object[capacity]; + System.arraycopy(elementData, 0, summaryArray, 0, size()); + System.arraycopy(arrayFromCollection, 0, summaryArray, size(), list.size()); + elementData = getNewCapacityArray(summaryArray); + size += list.size(); } @Override public T get(int index) { - return null; + if (index < size() && index >= 0) { + return (T) elementData[index]; + } else { + throw new ArrayListIndexOutOfBoundsException("This index doesn't exist"); + } } @Override public void set(T value, int index) { - + if (index < size() && index >= 0) /*&& elementData[index] != null)*/ { + elementData[index] = value; + } else { + throw new ArrayListIndexOutOfBoundsException("This index doesn't exist"); + } } @Override public T remove(int index) { - return null; + T deletedElement; + if (index < size() && index >= 0) { + deletedElement = (T) elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size() - index - 1); + elementData[size - 1] = null; + size--; + return deletedElement; + } + throw new ArrayListIndexOutOfBoundsException("This index doesn't exist"); } @Override public T remove(T element) { - return null; + for (int i = 0; i < size(); i++) { + if ((element == null && elementData[i] == null) + || element != null && element.equals(elementData[i])) { + return remove(i); + } + } + throw new NoSuchElementException("There is no element founded in this List"); } @Override public int size() { - return 0; + return size; } @Override public boolean isEmpty() { + if (size() == 0) { + return true; + } return false; } + + public Object[] getArrayFromCollection(List list) { + Object[] arrayFromCollection = new Object[list.size()]; + for (int i = 0; i < list.size(); i++) { + arrayFromCollection[i] = list.get(i); + } + return arrayFromCollection; + } + + public Object[] getNewCapacityArray(Object[] elementData) { + int capacityArray = elementData.length; + if (size() >= elementData.length) { + capacityArray += (capacityArray >> 1); + return Arrays.copyOf(elementData, capacityArray); + } + return elementData; + } + + @Override + public String toString() { + return "ArrayList{" + + "elementData=" + Arrays.toString(elementData) + + ", size = " + size + '}'; + } } From 5098947e9ee165884728d3e329dec794b8ffa89d Mon Sep 17 00:00:00 2001 From: Evgen Date: Sun, 24 May 2026 22:03:20 +0300 Subject: [PATCH 2/2] Added validate inde and check capacity methosd --- src/main/java/core/basesyntax/ArrayList.java | 50 ++++++++++++-------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 56239e46b..7865a0c2d 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,15 +1,15 @@ package core.basesyntax; -import java.util.Arrays; import java.util.NoSuchElementException; public class ArrayList implements List { - private Object[] elementData = new Object[10]; + private static final int DEFAULT_ARRAY_CAPACITY = 10; + private Object[] elementData = new Object[DEFAULT_ARRAY_CAPACITY]; private int size; @Override public void add(T value) { - elementData = getNewCapacityArray(elementData); + elementData = checkCapacity(elementData); elementData[size()] = value; size++; } @@ -19,7 +19,7 @@ public void add(T value, int index) { if (index > size() || index < 0) { throw new ArrayListIndexOutOfBoundsException("This index doesn't exist"); } - elementData = getNewCapacityArray(elementData); + elementData = checkCapacity(elementData); System.arraycopy(elementData, index, elementData, index + 1, size() - index); elementData[index] = value; size++; @@ -35,13 +35,14 @@ public void addAll(List list) { Object[] summaryArray = new Object[capacity]; System.arraycopy(elementData, 0, summaryArray, 0, size()); System.arraycopy(arrayFromCollection, 0, summaryArray, size(), list.size()); - elementData = getNewCapacityArray(summaryArray); + elementData = checkCapacity(summaryArray); size += list.size(); } @Override public T get(int index) { - if (index < size() && index >= 0) { + boolean rightIndex = validateIndex(index); + if (rightIndex == true) { return (T) elementData[index]; } else { throw new ArrayListIndexOutOfBoundsException("This index doesn't exist"); @@ -50,7 +51,8 @@ public T get(int index) { @Override public void set(T value, int index) { - if (index < size() && index >= 0) /*&& elementData[index] != null)*/ { + boolean rightIndex = validateIndex(index); + if (rightIndex == true) { elementData[index] = value; } else { throw new ArrayListIndexOutOfBoundsException("This index doesn't exist"); @@ -60,7 +62,8 @@ public void set(T value, int index) { @Override public T remove(int index) { T deletedElement; - if (index < size() && index >= 0) { + boolean rigthIndex = validateIndex(index); + if (rigthIndex == true) { deletedElement = (T) elementData[index]; System.arraycopy(elementData, index + 1, elementData, index, size() - index - 1); elementData[size - 1] = null; @@ -94,6 +97,21 @@ public boolean isEmpty() { return false; } + public Object[] checkCapacity(Object[] elementData) { + if (size() >= elementData.length) { + elementData = getNewCapacityArray(elementData); + return elementData; + } + return elementData; + } + + public boolean validateIndex(int index) { + if (index < size() && index >= 0) /*&& elementData[index] != null)*/ { + return true; + } + return false; + } + public Object[] getArrayFromCollection(List list) { Object[] arrayFromCollection = new Object[list.size()]; for (int i = 0; i < list.size(); i++) { @@ -104,17 +122,9 @@ public Object[] getArrayFromCollection(List list) { public Object[] getNewCapacityArray(Object[] elementData) { int capacityArray = elementData.length; - if (size() >= elementData.length) { - capacityArray += (capacityArray >> 1); - return Arrays.copyOf(elementData, capacityArray); - } - return elementData; - } - - @Override - public String toString() { - return "ArrayList{" - + "elementData=" + Arrays.toString(elementData) - + ", size = " + size + '}'; + capacityArray += (capacityArray >> 1); + Object[] newArray = new Object[capacityArray]; + System.arraycopy(elementData, 0, newArray, 0, size()); + return newArray; } }