-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Maksym Gorodnychyi #1784
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?
Maksym Gorodnychyi #1784
Changes from 1 commit
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,115 @@ | ||
| package core.basesyntax; | ||
|
|
||
| public class ArrayList<T> implements List<T> { | ||
| private static final int DEFAULT_CAPACITY = 10; | ||
| private T[] elements; | ||
| private int size; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public ArrayList() { | ||
| elements = (T[]) new Object[DEFAULT_CAPACITY]; | ||
| } | ||
|
|
||
| @Override | ||
| public void add(T value) { | ||
| if (size == elements.length) { | ||
| growArray(); | ||
|
|
||
| } | ||
| elements[size] = value; | ||
| size++; | ||
|
|
||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private void growArray() { | ||
| T[] newElements = (T[]) new Object[elements.length * 2]; | ||
| for (int i = 0; i < size; i++) { | ||
| newElements[i] = elements[i]; | ||
|
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. Checklist requires using |
||
| } | ||
| elements = newElements; | ||
| } | ||
|
|
||
| @Override | ||
| public void add(T value, int index) { | ||
|
|
||
| if (index < 0 || index > size) { | ||
| throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); | ||
|
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. Should throw |
||
| } | ||
| if (size == elements.length) { | ||
| growArray(); | ||
| } | ||
| for (int i = size; i > index; i--) { | ||
| elements[i] = elements[i - 1]; | ||
| } | ||
| elements[index] = value; | ||
| size++; | ||
| } | ||
|
|
||
| @Override | ||
| public void addAll(List<T> list) { | ||
| for (int i = 0; i < list.size(); i++) { | ||
| T value = list.get(i); | ||
| add(value); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private void checkIndex(int index) { | ||
| if (index < 0 || index >= size) { | ||
|
Comment on lines
+53
to
+54
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. Should throw |
||
| throw new IndexOutOfBoundsException("Index: " + index + ", Size" + size); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public T get(int index) { | ||
| return null; | ||
| checkIndex(index); | ||
| return elements[index]; | ||
| } | ||
|
|
||
| @Override | ||
| public void set(T value, int index) { | ||
| checkIndex(index); | ||
| elements[index] = value; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public T remove(int index) { | ||
| return null; | ||
| checkIndex(index); | ||
| T removedElement = elements[index]; | ||
| for (int i = index; i < size - 1; i++) { | ||
| elements[i] = elements[i + 1]; | ||
|
|
||
| } | ||
| size--; | ||
| elements[size] = null; | ||
| return removedElement; | ||
| } | ||
|
|
||
| @Override | ||
| public T remove(T element) { | ||
| for (int i = 0; i < size; i++) { | ||
|
|
||
|
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. Remove the redundant empty line inside the for-loop body. The loop iteration code should follow directly after the for statement.
Author
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. Done |
||
| if (element == null) { | ||
| if (elements[i] == null) { | ||
| return remove(i); | ||
| } | ||
| } else { | ||
| if (element.equals(elements[i])) { | ||
| return remove(i); | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
|
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 task requires throwing |
||
| } | ||
|
|
||
| @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.
Array grows at 2x (elements.length * 2), but the task requires 1.5x growth factor. Change this to use 1.5x multiplication.