Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #1554

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
95 changes: 95 additions & 0 deletions src/test/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package core.basesyntax;

import core.basesyntax.ArrayListIndexOutOfBoundsException;

Choose a reason for hiding this comment

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

The ArrayListIndexOutOfBoundsException is imported but not defined in the provided code. Ensure that this exception class is implemented or correctly imported to avoid runtime errors.

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

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

public ArrayList() {
elements = (E[]) new Object[DEFAULT_CAPACITY];
size = 0;
}

public void add(E value) {
ensureCapacity();
elements[size++] = value;
}

public void add(E value, int index) {
validateIndexForAdd(index);
ensureCapacity();
System.arraycopy(elements, index, elements, index + 1, size - index);
elements[index] = value;
size++;
}

public void addAll(ArrayList<E> list) {
if (list == null || list.isEmpty()) {
return;
}

for (int i = 0; i < list.size; i++) {
add(list.get(i));
}
}

public E remove(int index) {
validateIndex(index);
E removedElement = elements[index];
System.arraycopy(elements, index + 1, elements, index, size - index - 1);
elements[--size] = null;

return removedElement;
}

public E remove(E value) {
for (int i = 0; i < size; i++) {
if ((value == null && elements[i] == null) || (value != null && value.equals(elements[i]))) {
return remove(i);
}
}

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

public E get(int index) {
validateIndex(index);

return elements[index];
}

public void set(E value, int index) {
validateIndex(index);
elements[index] = value;
}

public int size() {
return size;
}

public boolean isEmpty() {
return size == 0;
}

private void ensureCapacity() {
if (size == elements.length) {
elements = Arrays.copyOf(elements, elements.length * 2);
Comment on lines +78 to +80

Choose a reason for hiding this comment

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

The ensureCapacity method currently doubles the array size. According to the task requirements, the array should grow by 1.5 times when it's full. Please adjust the growth factor to meet the requirement.

}
}

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

Choose a reason for hiding this comment

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

The ArrayListIndexOutOfBoundsException is used here, but it is not defined in the provided code. Ensure that this exception class is implemented or imported correctly to avoid runtime errors.

}
}

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

Choose a reason for hiding this comment

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

Similar to the previous comment, ensure that ArrayListIndexOutOfBoundsException is defined or imported correctly, as it is used here as well.

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package core.basesyntax;

public class ArrayListIndexOutOfBoundsException extends RuntimeException {
public ArrayListIndexOutOfBoundsException(String message) {
super(message);
}
}
Loading