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

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

@SuppressWarnings("unchecked")
public ArrayList() {
elements = (T[]) new Object[DEFAULT_CAPACITY];
}

@Override
public void add(T value) {
if (size == elements.length) {
growArray();

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

}

@SuppressWarnings("unchecked")
private void growArray() {
T[] newElements = (T[]) new Object[elements.length * 2];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Array grows at 2x (elements.length * 2), but the task requires 1.5x growth factor. Change this to use 1.5x multiplication.

for (int i = 0; i < size; i++) {
newElements[i] = 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.

Checklist requires using System.arraycopy() to move array elements, not a manual for-loop.

}
elements = newElements;
}

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

if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + 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.

Should throw ArrayListIndexOutOfBoundsException instead of IndexOutOfBoundsException to match task requirements.

}
if (size == elements.length) {
growArray();
}
for (int i = size; i > index; i--) {
elements[i] = elements[i - 1];
}
elements[index] = value;
size++;
}

@Override
public void addAll(List<T> list) {
for (int i = 0; i < list.size(); i++) {
T value = list.get(i);
add(value);
}

}

private void checkIndex(int index) {
if (index < 0 || index >= size) {
Comment on lines +53 to +54
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should throw ArrayListIndexOutOfBoundsException instead of IndexOutOfBoundsException to match task requirements.

throw new IndexOutOfBoundsException("Index: " + index + ", Size" + size);
}
}

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

@Override
public void set(T value, int index) {
checkIndex(index);
elements[index] = value;

}

@Override
public T remove(int index) {
return null;
checkIndex(index);
T removedElement = elements[index];
for (int i = index; i < size - 1; i++) {
elements[i] = elements[i + 1];

}
size--;
elements[size] = null;
return removedElement;
}

@Override
public T remove(T element) {
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.

Remove the redundant empty line inside the for-loop body. The loop iteration code should follow directly after the for statement.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

if (element == null) {
if (elements[i] == null) {
return remove(i);
}
} else {
if (element.equals(elements[i])) {
return remove(i);
}
}
}
return null;
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 task requires throwing NoSuchElementException when element is not found, but the method returns null instead.

}

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

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