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
109 changes: 100 additions & 9 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,139 @@
package core.basesyntax;

import java.util.NoSuchElementException;

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

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

public ArrayList(T[] elements) {
if (elements == null) {
throw new IllegalArgumentException("Array cannot be null");
}
this.elements = new Object[Math.max(DEFAULT_CAPACITY, elements.length)];
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 violates checklist item #2: 'Don't use Objects, Arrays, or any other util class'. The Math class is considered a utility class. You should implement this logic without using external library methods.

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 use of Math.max() is not allowed, as Math is a forbidden utility class according to checklist item #2. You should replace this with your own logic, for example, by using a ternary operator or an if-else statement.

int i = 0;
for (T elem : elements) {
this.elements[i] = elem;
i++;
}
Comment on lines +22 to +25
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

While this loop works correctly, the checklist recommends using System.arraycopy() to move array elements. Applying it here would be more efficient and consistent with the implementation of your other methods. [CHECKLIST ITEM #8]

this.size = elements.length;
}

private void grow() {
int newCapacity = elements.length + elements.length / 2;
Object[] newElements = new Object[newCapacity];
int i = 0;
for (Object newArray : this.elements) {
newElements[i] = newArray;
i++;
}
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 violates checklist item #8: 'Use System.arraycopy() to move your array elements'. A for loop is used to copy the elements to the new array, but the task requires you to use System.arraycopy() for this operation.

elements = newElements;
}

@Override
public void add(T value) {

if (this.size >= this.elements.length) {
grow();
}
elements[size] = value;
this.size++;
}

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

if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException("Index "
+ index + " out of bounds for size " + size);
}
Comment on lines +54 to +57
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 violates checklist item #5: 'Don't create repeating code'. The logic for checking if an index is out of bounds is repeated in the get(), set(), and remove(int) methods. This should be extracted into a separate private method to avoid duplication.

if (this.size >= this.elements.length) {
grow();
}
for (int i = size - 1; i >= index; i--) {
elements[i + 1] = elements[i];
}
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 violates checklist item #8: 'Use System.arraycopy() to move your array elements'. Instead of manually shifting elements in a loop, you should use a single call to System.arraycopy().

elements[index] = value;
size++;
}

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

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

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

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

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

@Override
public T remove(int index) {
return null;
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Index "
+ index + " out of bounds for size " + size);
}
final T removed = (T) this.elements[index];
for (int i = index; i < size - 1; i++) {
this.elements[i] = this.elements[i + 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.

This violates checklist item #8: 'Use System.arraycopy() to move your array elements'. This loop to shift elements after removal should be replaced with System.arraycopy() as required by the task.


}
elements[size - 1] = null;
size--;
return removed;
}

@Override
public T remove(T element) {
return null;
int index = -1;
for (int i = 0; i <= size - 1; i++) {
if (element == null) {
if (this.elements[i] == null) {
index = i;
break;
}
} else {
if (element.equals(this.elements[i])) {
index = i;
break;
}
}
Comment on lines +117 to +127
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 nested if-else structure can be simplified into a single if statement with a compound condition. This would make the code more concise and easier to read, in line with the guideline to avoid complicated if-else constructions. [CHECKLIST ITEM #4]

}
if (index == -1) {
throw new NoSuchElementException("Element not found: " + element);
}
return remove(index);
}

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

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