Skip to content
Open
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
98 changes: 90 additions & 8 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,130 @@
package core.basesyntax;

import java.util.NoSuchElementException;

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

@Override
public void add(T value) {

elementData = checkCapacity(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 = checkCapacity(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 = checkCapacity(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;
boolean rightIndex = validateIndex(index);
if (rightIndex == true) {
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.


boolean rightIndex = validateIndex(index);
if (rightIndex == true) {
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;
boolean rigthIndex = validateIndex(index);
if (rigthIndex == true) {
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[] checkCapacity(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.

This violates checklist item #7: These helper methods (checkCapacity, validateIndex, getArrayFromCollection, getNewCapacityArray) are not part of the List interface and should be private, not public.

if (size() >= elementData.length) {
elementData = getNewCapacityArray(elementData);
return elementData;
}
return elementData;
Comment on lines +100 to +105
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 checkCapacity method creates redundant assignments. Since the method returns a value either way, you can simply return the result directly without the intermediate variable assignment.

}

public boolean validateIndex(int index) {
if (index < size() && index >= 0) /*&& elementData[index] != null)*/ {
return true;
}
return false;
Comment on lines +108 to +112
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 validateIndex() method should throw the exception directly instead of returning boolean, eliminating the if-else pattern in calling methods. Compare to the 'Bad example' in the checklist.

}

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;
capacityArray += (capacityArray >> 1);
Object[] newArray = new Object[capacityArray];
System.arraycopy(elementData, 0, newArray, 0, size());
return newArray;
}
}
Loading