Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 88 additions & 8 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,128 @@
package core.basesyntax;

import java.util.NoSuchElementException;
import java.util.Objects;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The checklist explicitly states 'Don't use Objects, Arrays, or any other util class.' This import must be removed.


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

private T[] data;
private int size;

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

public ArrayList(T[] elementData, int size) {
if (elementData == null) {
throw new IllegalArgumentException("elementData is null");
}
if (size < 0 || size > elementData.length) {
throw new IllegalArgumentException("Invalid size: " + size);
}
this.data = elementData;
this.size = size;
}

@Override
public void add(T value) {

if (size == data.length) {
growIfArrayFull();
}
data[size] = value;
size++;
}

@Override
public void add(T value, int index) {
if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException(
"Index " + index + " is out of bounds for size " + size);
}

if (size == data.length) {
growIfArrayFull();
}

System.arraycopy(data, index, data, index + 1, size - index);
data[index] = value;
size++;
}

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

if (list == null) {
throw new IllegalArgumentException("list is null");
}
for (int i = 0; i < list.size(); i++) {
add(list.get(i));
}
}

@Override
public T get(int index) {
return null;
checkIndex(index);
return data[index];
}

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

checkIndex(index);
data[index] = value;
}

@Override
public T remove(int index) {
return null;
checkIndex(index);

final T removed = data[index];

int elementsToMove = size - index - 1;
if (elementsToMove > 0) {
System.arraycopy(data, index + 1, data, index, elementsToMove);
}

data[size - 1] = null;
size--;

return removed;
}

@Override
public T remove(T element) {
return null;
for (int i = 0; i < size; i++) {
if (Objects.equals(data[i], element)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace Objects.equals(data[i], element) with manual null-safe equality check. For example: (data[i] == null ? element == null : data[i].equals(element))

T removed = data[i];
remove(i);
return removed;
}
}
throw new NoSuchElementException("element not found");
}

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

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

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

private void growIfArrayFull() {
int newCapacity = (int) (data.length * GROWTH_FACTOR);
T[] newData = (T[]) new Object[newCapacity];
System.arraycopy(data, 0, newData, 0, size);
data = newData;
}
}
Loading