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

import java.util.Arrays;
import java.util.NoSuchElementException;

public class ArrayList<T> implements List<T> {
private Object[] elementData = new Object[10];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Magic number 10 should be a constant per checklist item #3. Create a constant like DEFAULT_CAPACITY = 10 and use it here.

private int size;

@Override
public void add(T value) {

elementData = getNewCapacityArray(elementData);
elementData[size()] = value;
size++;
}

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

if (index > size() || index < 0) {
throw new ArrayListIndexOutOfBoundsException("This index doesn't exist");
}
elementData = getNewCapacityArray(elementData);
System.arraycopy(elementData, index, elementData, index + 1, size() - index);
elementData[index] = value;
size++;
}

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

if (list.size() == 0) {
return;
}
Object[] arrayFromCollection = getArrayFromCollection(list);
int capacity = list.size() + size();
Object[] summaryArray = new Object[capacity];
System.arraycopy(elementData, 0, summaryArray, 0, size());
System.arraycopy(arrayFromCollection, 0, summaryArray, size(), list.size());
elementData = getNewCapacityArray(summaryArray);
size += list.size();
}

@Override
public T get(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.

Per checklist item #5: The index validation logic index < size() && index >= 0 is repeated in get(), set(), and remove(int index) methods. Move this to a private helper method like validateIndex(int index).

return null;
if (index < size() && index >= 0) {
return (T) elementData[index];
} else {
throw new ArrayListIndexOutOfBoundsException("This index doesn't exist");
}
Comment on lines 42 to +49
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: The get() method has unnecessary if-else after validateIndex(). Same issue applies to set() and remove(int) methods.

}

@Override
public void set(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.

Per checklist item #5: Same index validation repeated here - should use the shared private method.


if (index < size() && index >= 0) /*&& elementData[index] != null)*/ {
elementData[index] = value;
} else {
throw new ArrayListIndexOutOfBoundsException("This index doesn't exist");
}
Comment on lines 52 to +59
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: The set() method has unnecessary if-else after validateIndex().

}

@Override
public T remove(int index) {
return null;
T deletedElement;
if (index < size() && index >= 0) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Per checklist item #5: Same index validation repeated here - should use the shared private method.

deletedElement = (T) elementData[index];
System.arraycopy(elementData, index + 1, elementData, index, size() - index - 1);
elementData[size - 1] = null;
size--;
return deletedElement;
}
throw new ArrayListIndexOutOfBoundsException("This index doesn't exist");
Comment on lines 62 to +73
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: The remove(int) method has unnecessary if-else after validateIndex().

}

@Override
public T remove(T element) {
return null;
for (int i = 0; i < size(); 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 #6: 'Do not use one-letter names'. The variable 'i' is used in loops. While this is common in Java, consider if more descriptive iteration variable names would help.

if ((element == null && elementData[i] == null)
|| element != null && element.equals(elementData[i])) {
return remove(i);
}
}
throw new NoSuchElementException("There is no element founded in this List");
}

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

@Override
public boolean isEmpty() {
Comment on lines 92 to 93
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Per checklist item #4: This if-else can be simplified to return size() == 0;

if (size() == 0) {
return true;
}
return false;
Comment on lines 92 to 97
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 #4: 'Don't complicate if-else construction'. The if-else can be replaced with a simple return statement.

}

public Object[] getArrayFromCollection(List<T> list) {
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 #6: 'Don't use abstract words like array as variable names'. Consider renaming 'arrayFromCollection' to something more informative like 'elementsFromCollection'.

Object[] arrayFromCollection = new Object[list.size()];
for (int i = 0; i < list.size(); i++) {
arrayFromCollection[i] = list.get(i);
}
return arrayFromCollection;
}

public Object[] getNewCapacityArray(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.

Per checklist item #8: The getNewCapacityArray method both checks if resize is needed AND creates the new array. The growth check is currently done outside the method in add(), causing unnecessary array copying on every add. Consider having the method only handle growth when needed, or ensure the method itself determines when growth is necessary.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Parameter name shadows the field name elementData. Use a different name like sourceArray.

int capacityArray = elementData.length;
if (size() >= elementData.length) {
capacityArray += (capacityArray >> 1);
return Arrays.copyOf(elementData, capacityArray);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Per checklist item #2: Do not use Arrays utility class. Replace Arrays.copyOf() with manual copying logic.

}
return elementData;
}

@Override
public String toString() {
return "ArrayList{"
+ "elementData=" + Arrays.toString(elementData)
+ ", size = " + size + '}';
}
}
Loading