-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: master
Are you sure you want to change the base?
solution #1554
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package core.basesyntax; | ||
|
||
import core.basesyntax.ArrayListIndexOutOfBoundsException; | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
} | ||
|
||
private void validateIndex(int index) { | ||
if (index < 0 || index >= size) { | ||
throw new ArrayListIndexOutOfBoundsException("Index out of bounds: " + index); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
} | ||
|
||
private void validateIndexForAdd(int index) { | ||
if (index < 0 || index > size) { | ||
throw new ArrayListIndexOutOfBoundsException("Index out of bounds for add: " + index); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the previous comment, ensure that |
||
} | ||
} | ||
} |
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); | ||
} | ||
} |
There was a problem hiding this comment.
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.