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
114 changes: 105 additions & 9 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,144 @@
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[DEFAULT_CAPACITY < elements.length
? elements.length : DEFAULT_CAPACITY];
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];
System.arraycopy(elements, 0, newElements, 0, size);
elements = newElements;
}

private void arrayListIndexOutOfBoundException(int index) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Index "
+ index + " out of bounds for size " + size);
}
}

@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();
}
System.arraycopy(
this.elements,
index,
this.elements,
index + 1,
size - index
);
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;
arrayListIndexOutOfBoundException(index);
return (T) elements[index];
}

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

arrayListIndexOutOfBoundException(index);
elements[index] = value;
}

@Override
public T remove(int index) {
return null;
arrayListIndexOutOfBoundException(index);
final T removed = (T) this.elements[index];
int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(
elements,
index + 1,
elements,
index,
numMoved
);
}
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