From 1cd445c91bab792a53b18667bf4b367f840ad09b Mon Sep 17 00:00:00 2001 From: Tony Hut Date: Sun, 29 Oct 2023 16:40:05 +0100 Subject: [PATCH 1/2] implemented class ArrayList --- src/main/java/core/basesyntax/ArrayList.java | 77 +++++++++++++++++--- 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 8d6477943..d602cec23 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,48 +1,107 @@ package core.basesyntax; +import java.util.NoSuchElementException; + public class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 10; + private static final double MIN_CAPACITY = 1.5; + private Object[] elementData; + private int size; + + public ArrayList() { + elementData = new Object[DEFAULT_CAPACITY]; + } + @Override public void add(T value) { - + growIfArrayFull(); + elementData[size] = value; + size++; } @Override public void add(T value, int index) { - + if (index < 0 || index > size) { + throw new ArrayListIndexOutOfBoundsException("incorrect index"); + } + growIfArrayFull(); + System.arraycopy(elementData, index, elementData, index + 1, size - index); + elementData[index] = value; + size++; } @Override public void addAll(List list) { - + growIfArrayFull(); + for (int i = 0; i < list.size(); i++) { + growIfArrayFull(); + this.elementData[size] = list.get(i); + size++; + } } @Override public T get(int index) { - return null; + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException("incorrect index"); + } + return (T) elementData[index]; } @Override public void set(T value, int index) { - + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException("incorrect index"); + } + elementData[index] = value; } @Override public T remove(int index) { - return null; + if (index < 0 || index >= size) { + throw new ArrayListIndexOutOfBoundsException("incorrect index"); + } + Object removedElement = elementData[index]; + System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); + size--; + return (T) removedElement; } @Override public T remove(T element) { - return null; + int position = index(element); + if (position < 0) { + throw new NoSuchElementException("element does not exist"); + } + Object removedElement = remove(position); + return (T) removedElement; } @Override public int size() { - return 0; + return size; } @Override public boolean isEmpty() { - return false; + return size == 0; + } + + public int index(T element) { + for (int i = 0; i < size; i++) { + if ((element == elementData[i]) + || (element != null && element.equals(elementData[i]))) { + return i; + } + } + return -1; + } + + private void growIfArrayFull() { + if (elementData.length == size) { + Object[] newArray = new Object[(int) (elementData.length * MIN_CAPACITY)]; + System.arraycopy(elementData, 0, newArray, 0, size); + elementData = newArray; + } } } From 481e74a3253a554ad6f678d5efb84a10a4d8d593 Mon Sep 17 00:00:00 2001 From: Tony Hut Date: Mon, 30 Oct 2023 09:48:19 +0100 Subject: [PATCH 2/2] fixed errors --- src/main/java/core/basesyntax/ArrayList.java | 32 +++++++++----------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index d602cec23..526b392ab 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -4,7 +4,7 @@ public class ArrayList implements List { private static final int DEFAULT_CAPACITY = 10; - private static final double MIN_CAPACITY = 1.5; + private static final double GROWING_FACTOR = 1.5; private Object[] elementData; private int size; @@ -32,35 +32,26 @@ public void add(T value, int index) { @Override public void addAll(List list) { - growIfArrayFull(); for (int i = 0; i < list.size(); i++) { - growIfArrayFull(); - this.elementData[size] = list.get(i); - size++; + add(list.get(i)); } } @Override public T get(int index) { - if (index < 0 || index >= size) { - throw new ArrayListIndexOutOfBoundsException("incorrect index"); - } + indexChecker(index); return (T) elementData[index]; } @Override public void set(T value, int index) { - if (index < 0 || index >= size) { - throw new ArrayListIndexOutOfBoundsException("incorrect index"); - } + indexChecker(index); elementData[index] = value; } @Override public T remove(int index) { - if (index < 0 || index >= size) { - throw new ArrayListIndexOutOfBoundsException("incorrect index"); - } + indexChecker(index); Object removedElement = elementData[index]; System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); size--; @@ -69,7 +60,7 @@ public T remove(int index) { @Override public T remove(T element) { - int position = index(element); + int position = getIndex(element); if (position < 0) { throw new NoSuchElementException("element does not exist"); } @@ -87,7 +78,7 @@ public boolean isEmpty() { return size == 0; } - public int index(T element) { + private int getIndex(T element) { for (int i = 0; i < size; i++) { if ((element == elementData[i]) || (element != null && element.equals(elementData[i]))) { @@ -99,9 +90,16 @@ public int index(T element) { private void growIfArrayFull() { if (elementData.length == size) { - Object[] newArray = new Object[(int) (elementData.length * MIN_CAPACITY)]; + Object[] newArray = new Object[(int) (elementData.length * GROWING_FACTOR)]; System.arraycopy(elementData, 0, newArray, 0, size); elementData = newArray; } } + + private void indexChecker(int index) { + if ((index < 0 || index >= size)) { + throw new ArrayListIndexOutOfBoundsException("incorrect index"); + } + + } }