-
Notifications
You must be signed in to change notification settings - Fork 1.6k
solved task #1767
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
base: master
Are you sure you want to change the base?
solved task #1767
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 |
|---|---|---|
| @@ -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--; | ||
| return removed; | ||
|
Comment on lines
+97
to
+103
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. This block of code duplicates the logic from the Instead of duplicating the removal logic here, this method should find the index of the element ( |
||
| } | ||
| } | ||
| throw new NoSuchElementException("Element not found: " + element); | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return 0; | ||
| return size; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEmpty() { | ||
| return false; | ||
| return (size == 0); | ||
| } | ||
| } | ||
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.
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 callremove(index)to perform the actual removal. This will eliminate the repeated code, following the 'Don't Repeat Yourself' principle mentioned in checklist item #5.