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

import java.util.NoSuchElementException;

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

public ArrayList() {
size = 0;
elements = (T[]) new Object[CAPACITY];
}

public void resize() {
int newCapacity = (int) (elements.length * GROWTH_FACTOR);
T[] newArray = (T[]) new Object[newCapacity ];

System.arraycopy(elements, 0, newArray, 0, size);

elements = newArray;
}

private void validateIndex(int index) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("Index " + index);
}
}

@Override
public void add(T value) {

if (size == elements.length) {
resize();
}
elements[size] = value;
size++;
}

@Override
public void add(T value, int index) {
if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException("Index " + index);
}

if (size == elements.length) {
resize();
}

System.arraycopy(elements, index, elements, index + 1, size - index);

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

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

for (int i = 0; i < list.size(); i++) {
if (size == elements.length) {
resize();
}
elements[size] = list.get(i);
size++;
}
}

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

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

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

@Override
public T remove(int index) {
return null;
validateIndex(index);

final T removed = elements[index];

System.arraycopy(elements, index + 1, elements, index, size - index - 1);

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

@Override
public T remove(T element) {
return null;
for (int i = 0; i < size; i++) {
if ((elements[i] == null && element == null)
|| (elements[i] != null && elements[i].equals(element))) {
final T removed = elements[i];

System.arraycopy(elements, i + 1, elements, i, size - i - 1);

elements[size - 1] = null;
size--;
Comment on lines +99 to +102
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 block of code for removing an element is a duplicate of the logic in your remove(int index) method. A better approach would be to find the index of the element you want to remove and then call remove(index) to perform the actual removal. This will eliminate the repeated code, following the 'Don't Repeat Yourself' principle mentioned in checklist item #5.

return removed;
Comment on lines +97 to +103
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 block of code duplicates the logic from the remove(int index) method. This violates a key requirement to avoid repeating code (Checklist item #5).

Instead of duplicating the removal logic here, this method should find the index of the element (i) and then call the other remove method to do the work, like return remove(i);.

}
}
throw new NoSuchElementException("Element not found: " + element);
}

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

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