From 2f72a324950007733b1d521290657b80edd78aaa Mon Sep 17 00:00:00 2001 From: Dmytro Borysenko Date: Fri, 22 Nov 2024 22:08:25 +0200 Subject: [PATCH] https://github.com/DmytroBorysenko2004/jv-array-list --- src/main/java/core/basesyntax/ArrayList.java | 99 +++++++++++++++++--- 1 file changed, 84 insertions(+), 15 deletions(-) diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index 8d6477943..4af6ac866 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -1,48 +1,117 @@ package core.basesyntax; -public class ArrayList implements List { - @Override - public void add(T value) { +import java.util.Arrays; +import java.util.List; +import java.util.NoSuchElementException; + +public abstract class ArrayList implements List { + private static final int DEFAULT_CAPACITY = 10; + private Object[] elements; + private int size; + public ArrayList() { + elements = new Object[DEFAULT_CAPACITY]; + size = 0; } - @Override - public void add(T value, int index) { + private void ensureCapacity(int minCapacity) { + if (minCapacity > elements.length) { + int newCapacity = elements.length * 3 / 2 + 1; + elements = Arrays.copyOf(elements, newCapacity); + } + } + private void rangeCheck(int index) { + if (index >= size || index < 0) { + throw new ArrayListIndexOutOfBoundsException("Index: " + index + ", Size: " + size); + } } @Override - public void addAll(List list) { + public boolean add(T value) { + ensureCapacity(size + 1); + elements[size++] = value; + return true; + } + + public void add(T value, int index) { + rangeCheck(index); + ensureCapacity(size + 1); + System.arraycopy(elements, index, elements, index + 1, size - index); + elements[index] = value; + size++; + } + public void addAll(List list) { + ensureCapacity(size + list.size()); + for (T element : list) { + add(element); + } } @Override public T get(int index) { - return null; + rangeCheck(index); + return (T) elements[index]; } - @Override public void set(T value, int index) { - + rangeCheck(index); + elements[index] = value; } @Override public T remove(int index) { - return null; + rangeCheck(index); + T oldValue = (T) elements[index]; + int numMoved = size - index - 1; + if (numMoved > 0) { + System.arraycopy(elements, index + 1, elements, index, numMoved); + } + elements[--size] = null; + return oldValue; } - @Override - public T remove(T element) { - return null; + public boolean remove(Object element) { + int index = indexOf(element); + if (index >= 0) { + remove(index); + return true; + } else { + throw new NoSuchElementException("Element not found"); + } } @Override public int size() { - return 0; + return size; } @Override public boolean isEmpty() { - return false; + return size == 0; + } + + public int indexOf(Object o) { + if (o == null) { + for (int i = 0; i < size; i++) { + if (elements[i] == null) { + return i; + } + } + } else { + for (int i = 0; i < size; i++) { + if (o.equals(elements[i])) { + return i; + } + } + } + return -1; + } + + public static class ArrayListIndexOutOfBoundsException extends IndexOutOfBoundsException { + public ArrayListIndexOutOfBoundsException(String s) { + super(s); + } } }