Skip to content

Commit

Permalink
Implemented all needed methods in ArrayList class, used necessary exc…
Browse files Browse the repository at this point in the history
…eptions for "out of bound" or "no such element" events.
  • Loading branch information
gekatonheyr committed Dec 30, 2024
1 parent 798d9ef commit 82a618b
Showing 1 changed file with 71 additions and 9 deletions.
80 changes: 71 additions & 9 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,110 @@
package core.basesyntax;

import java.util.NoSuchElementException;

public class ArrayList<T> implements List<T> {
public static final int DEFAULT_CAPACITY = 10;
public static final double RESIZE_FACTOR = 1.5;

private T[] elements;
private int size;

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

private void ensureCapacity() {
if (size == this.elements.length) {
this.resize((int) (elements.length * RESIZE_FACTOR));
}
}

private void resize(int newCapacity) {
T[] elements = (T[]) new Object[newCapacity];
System.arraycopy(this.elements, 0, elements, 0, size);
this.elements = elements;
}

@Override
public void add(T value) {

this.ensureCapacity();
this.elements[size] = value;
this.size++;
}

@Override
public void add(T value, int index) {

if (index < 0 || index > this.size) {
throw new ArrayListIndexOutOfBoundsException("Index out of bound.");
}
this.ensureCapacity();
T[] swap = (T[]) new Object[size - index];
System.arraycopy(this.elements, index, swap, 0, size - index);
this.elements[index] = value;
System.arraycopy(swap, 0, this.elements, index + 1, size - index);
this.size++;
}

@Override
public void addAll(List<T> list) {

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

@Override
public T get(int index) {
return null;
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Index out of bounds");
}
return elements[index];
}

@Override
public void set(T value, int index) {

if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Index out of bounds");
}
elements[index] = value;
}

@Override
public T remove(int index) {
return null;
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Index out of bounds");
}
T swap = elements[index];
System.arraycopy(this.elements, index + 1, this.elements, index, size - index - 1);
size--;
return swap;
}

@Override
public T remove(T element) {
return null;
for (int i = 0; i < size; i++) {
if (elements[i] == null && element != null) {
continue;
}
if (element == null && elements[i] == null) {
this.remove(i);
return null;
}
if (elements[i].equals(element)) {
this.remove(i);
return element;
}
}
throw new NoSuchElementException("No such element for removing");
}

@Override
public int size() {
return 0;
return size;
}

@Override
public boolean isEmpty() {
return false;
return this.size() == 0;
}
}

0 comments on commit 82a618b

Please sign in to comment.