Skip to content
Open

f #1780

Changes from all commits
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
70 changes: 63 additions & 7 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,104 @@
package core.basesyntax;

import java.util.NoSuchElementException;

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is no resize/grow helper method. The checklist explicitly requires: "Resize the array in a separate method." Add a private method that grows elementData by 1.5x (matching java.util.ArrayList behavior) and use System.arraycopy() to copy elements.

public ArrayList() {
elementData = (T[]) new Object[DEFAULT_CAPACITY];
capacity = DEFAULT_CAPACITY;
}

@Override
public void add(T value) {

if (size >= capacity) {
grow();
}
elementData[size] = value;
size++;
}

@Override
public void add(T value, int index) {
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 method signature here is add(T value, int index), but the task explicitly refers to add(int index, T value) (the indexed-add should follow the required order). Ensure the method signature matches the requirement/interface and the documented API.

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 indexed add method uses the signature add(T value, int index). This violates the requirement: "The indexed add method must: Use the signature add(int index, T value) (matching the List interface)." Change the method signature to add(int index, T value) so it matches the List interface and expected API.

if (index > size || index < START_INDEX) {
throw new ArrayListIndexOutOfBoundsException("index %s out of bounds for length %s"
.formatted(index, size));
}
Comment on lines +28 to +31
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You perform index validation here manually instead of reusing the index-check helper. The requirements state you should move repeated index checks to a private helper and use it for add(int index, T value) as well. Also keep in mind that adding at index == size is allowed (the checker must allow upper bound == size).

Comment on lines +28 to +31
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This block performs its own index validation instead of reusing the shared index-check helper. The requirements state: "Index checking logic that is shared by methods like get(), set(), and remove() must be moved to a separate private method to avoid repeating code" and "The indexed add method must: Use a shared index-check helper method instead of its own index validation." Refactor so the indexed add calls a common helper (or a helper variant) that allows index == size for insertion at the end.

if (size >= capacity) {
grow();
}
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = value;
size++;

}

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

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

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

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

elementData[index] = value;
}

@Override
public T remove(int index) {
return null;
checkIndex(index);
T value = elementData[index];
System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
size--;
Comment on lines +65 to +66
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

After shifting elements and decrementing size you must null out the former last element position to avoid memory leaks and ensure toString()/iteration don't see stale data. The requirements say: "When an element is removed, shift size - index - 1 elements to close the gap using System.arraycopy. Decrement size. Set the former last element position to null." Add elementData[size] = null; after size--.

return value;
}

@Override
public T remove(T element) {
return null;
for (int i = 0; i < size; i++) {
if (element == elementData[i]
|| (elementData[i] != null && elementData[i].equals(element))) {
return remove(i);
}
}
throw new NoSuchElementException("Element not found: " + element);
}

@Override
public int size() {
return 0;
return size;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

size() is implemented correctly returning size, but other methods that rely on it are unimplemented; ensure consistency when you implement add/remove methods.

}

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

private void grow() {
capacity += capacity / 2;
T[] newElementData = (T[]) new Object[capacity];
System.arraycopy(elementData, START_INDEX, newElementData, START_INDEX, size);
elementData = newElementData;
}

private void checkIndex(int index) {
if (index > size - 1 || index < START_INDEX) {
throw new ArrayListIndexOutOfBoundsException("index %s out of bounds for length %s"
.formatted(index, size));
}
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There is no toString() implementation. The checklist requires: "toString() implementation must: Not use Arrays.toString. Iterate only over indices 0 to size - 1. Build the string manually (for example, using StringBuilder) so output matches java.util.ArrayList and does not print unused null entries." Implement toString() accordingly so output matches java.util.ArrayList and doesn't include unused null slots.

Loading