Skip to content
Open

f #1780

Show file tree
Hide file tree
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
15 changes: 12 additions & 3 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package core.basesyntax;

public class ArrayList<T> implements List<T> {
@Override
public void add(T value) {

private static final int DEFAULT_SIZE = 10;
private Object[] elementData;
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 internal storage elementData is declared but never initialized (no constructor). Initialize it to new Object[DEFAULT_SIZE] in a constructor so the list has the default capacity required by the task.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Main.java incorrectly imports java.util.ArrayList which will shadow your core.basesyntax.ArrayList. Also main is incomplete: the line ArrayList alone is invalid. Either import/use your implementation or fully qualify the intended class and finish the example usage.

@Override
public void add(int index, T value) {
if (elementData.length == 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.

The implementation never throws the required custom exception for invalid indexes. The description requires: "Throw custom ArrayListIndexOutOfBoundsException in case the index passed to any of the methods is invalid." Add and use this exception with an informative message in your index-validation method.

elementData = ;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Syntax error: elementData = ; is an invalid assignment and prevents compilation. Implement resizing via a separate method and assign its result here (see checklist: "Resize the array in a separate method.").

}
elementData[index] = value;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Overall missing use of System.arraycopy() for shifting/copying elements. Per checklist: "Use System.arraycopy() to move your array elements." Replace manual loops (or absent logic) with System.arraycopy() in insert/remove/grow operations.

size = size + 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

add(int index, T value) lacks index validation, resizing, and element-shifting logic. You must check the index (and throw an informative ArrayListIndexOutOfBoundsException on invalid index), grow the array when full, and shift elements using System.arraycopy() before inserting the value (see checklist: "Use System.arraycopy() to move your array elements.").

}

@Override
Expand Down Expand Up @@ -38,7 +47,7 @@ public T remove(T 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
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package core.basesyntax;

import java.util.ArrayList;
import java.util.List;

public class Main {
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 internal storage elementData is declared but never initialized and DEFAULT_SIZE is not used. You need a constructor that initializes elementData = new Object[DEFAULT_SIZE] so the list has the required default capacity (same as java.util.ArrayList).

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 import violates the checklist: "Don't use Objects, Arrays, or any other util class." You also use Arrays.copyOf and Arrays.toString below; remove this import and avoid java.util.Arrays usage in favor of allocating a new backing array and using System.arraycopy (and building toString using elements up to size).

public static void main(String[] args) {
ArrayList
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Checklist requirement: "Resize the array in a separate method." There is no resize/grow method in this class. Add a private method to handle resizing, computing new capacity as oldCapacity + (oldCapacity >> 1) (1.5x) and using System.arraycopy() to copy elements.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Main.java imports java.util.ArrayList which will shadow your core.basesyntax.ArrayList. Also the main body is incomplete (ArrayList on its own). Remove or fix the import and instantiate your implementation correctly, e.g. core.basesyntax.ArrayList<Type> list = new core.basesyntax.ArrayList<>(); and complete the main method.

}
}
Loading