Skip to content

Commit

Permalink
array
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaPryt committed Dec 11, 2024
1 parent b897f43 commit 489cd43
Showing 1 changed file with 58 additions and 39 deletions.
97 changes: 58 additions & 39 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,27 @@
import java.util.NoSuchElementException;

public class ArrayList<T> implements List<T> {
private static final int DEFAULT_CAPACITY = 10;
private T[] elements;
private int size;
private static final int DEFAULT_CAPACITY = 10;
private static final int START_SIZE = 0;

public ArrayList() {
elements = (T[]) new Object[DEFAULT_CAPACITY];
size = START_SIZE;
size = 0;
}

@Override
public void add(T value) {
if (size == elements.length) {
int newCapacity = (int) (elements.length * 1.5);
elements = Arrays.copyOf(elements, newCapacity);
}
growIfNeeded();
elements[size] = value;
size++;
}

@Override
public void add(T value, int index) {
if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index);
}

if (size == elements.length) {
int newCapacity = (int) (elements.length * 1.5);
elements = Arrays.copyOf(elements, newCapacity);
}

for (int i = size; i > index; i--) {
elements[i] = elements[i - 1];
}
checkIndexForAdd(index);
growIfNeeded();
shiftRight(index);
elements[index] = value;
size++;
}
Expand All @@ -47,50 +34,38 @@ public void addAll(List<T> list) {
if (list == null) {
throw new NullPointerException("The provided list is null");
}

ensureCapacity(size + list.size());
for (int i = 0; i < list.size(); i++) {
this.add(list.get(i));
add(list.get(i));
}
}

@Override
public T get(int index) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index);
}
checkIndex(index);
return elements[index];
}

@Override
public void set(T value, int index) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index);
}
checkIndex(index);
elements[index] = value;
}

@Override
public T remove(int index) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index);
}

checkIndex(index);
T removedElement = elements[index];

for (int i = index; i < size - 1; i++) {
elements[i] = elements[i + 1];
}

elements[size - 1] = null;
shiftLeft(index);
size--;

return removedElement;
}

@Override
public T remove(T element) {
for (int i = 0; i < size; i++) {
if (elements[i].equals(element)) {
if ((elements[i] == null && element == null) || (elements[i]
!= null && elements[i].equals(element))) {
return remove(i);
}
}
Expand All @@ -106,4 +81,48 @@ public int size() {
public boolean isEmpty() {
return size == 0;
}

private void growIfNeeded() {
if (size == elements.length) {
int newCapacity = (int) (elements.length * 1.5);
elements = Arrays.copyOf(elements, newCapacity);
}
}

private void ensureCapacity(int minCapacity) {
if (minCapacity > elements.length) {
int newCapacity = elements.length;
while (newCapacity < minCapacity) {
newCapacity = (int) (newCapacity * 1.5);
}
elements = Arrays.copyOf(elements, newCapacity);
}
}

private void shiftRight(int index) {
for (int i = size; i > index; i--) {
elements[i] = elements[i - 1];
}
}

private void shiftLeft(int index) {
for (int i = index; i < size - 1; i++) {
elements[i] = elements[i + 1];
}
elements[size - 1] = null;
}

private void checkIndex(int index) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Index " + index
+ " is out of bounds. Size: " + size);
}
}

private void checkIndexForAdd(int index) {
if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException("Index " + index
+ " is out of bounds for add. Size: " + size);
}
}
}

0 comments on commit 489cd43

Please sign in to comment.