-
Notifications
You must be signed in to change notification settings - Fork 1.6k
task competed #1770
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?
task competed #1770
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,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++; | ||
| } | ||
| 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
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 violates checklist item #5: 'Don't create repeating code'. The logic for checking if an index is out of bounds is repeated in the |
||
| 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
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 nested |
||
| } | ||
| 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; | ||
| } | ||
| } | ||
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.
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]