-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implemented methods for ArrayList class #1793
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?
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,120 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| public class ArrayList<T> implements List<T> { | ||
| private Object[] elementData = new Object[10]; | ||
| private int size; | ||
|
|
||
| @Override | ||
| public void add(T value) { | ||
|
|
||
| elementData = getNewCapacityArray(elementData); | ||
| elementData[size()] = value; | ||
| size++; | ||
| } | ||
|
|
||
| @Override | ||
| public void add(T value, int index) { | ||
|
|
||
| if (index > size() || index < 0) { | ||
| throw new ArrayListIndexOutOfBoundsException("This index doesn't exist"); | ||
| } | ||
| elementData = getNewCapacityArray(elementData); | ||
| System.arraycopy(elementData, index, elementData, index + 1, size() - index); | ||
| elementData[index] = value; | ||
| size++; | ||
| } | ||
|
|
||
| @Override | ||
| public void addAll(List<T> list) { | ||
|
|
||
| if (list.size() == 0) { | ||
| return; | ||
| } | ||
| Object[] arrayFromCollection = getArrayFromCollection(list); | ||
| int capacity = list.size() + size(); | ||
| Object[] summaryArray = new Object[capacity]; | ||
| System.arraycopy(elementData, 0, summaryArray, 0, size()); | ||
| System.arraycopy(arrayFromCollection, 0, summaryArray, size(), list.size()); | ||
| elementData = getNewCapacityArray(summaryArray); | ||
| size += list.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public T get(int 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. Per checklist item #5: The index validation logic |
||
| return null; | ||
| if (index < size() && index >= 0) { | ||
| return (T) elementData[index]; | ||
| } else { | ||
| throw new ArrayListIndexOutOfBoundsException("This index doesn't exist"); | ||
| } | ||
|
Comment on lines
42
to
+49
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: The get() method has unnecessary if-else after validateIndex(). Same issue applies to set() and remove(int) methods. |
||
| } | ||
|
|
||
| @Override | ||
| public void set(T value, int 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. Per checklist item #5: Same index validation repeated here - should use the shared private method. |
||
|
|
||
| if (index < size() && index >= 0) /*&& elementData[index] != null)*/ { | ||
| elementData[index] = value; | ||
| } else { | ||
| throw new ArrayListIndexOutOfBoundsException("This index doesn't exist"); | ||
| } | ||
|
Comment on lines
52
to
+59
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: The set() method has unnecessary if-else after validateIndex(). |
||
| } | ||
|
|
||
| @Override | ||
| public T remove(int index) { | ||
| return null; | ||
| T deletedElement; | ||
| if (index < size() && index >= 0) { | ||
|
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. Per checklist item #5: Same index validation repeated here - should use the shared private method. |
||
| deletedElement = (T) elementData[index]; | ||
| System.arraycopy(elementData, index + 1, elementData, index, size() - index - 1); | ||
| elementData[size - 1] = null; | ||
| size--; | ||
| return deletedElement; | ||
| } | ||
| throw new ArrayListIndexOutOfBoundsException("This index doesn't exist"); | ||
|
Comment on lines
62
to
+73
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: The remove(int) method has unnecessary if-else after validateIndex(). |
||
| } | ||
|
|
||
| @Override | ||
| public T remove(T element) { | ||
| return null; | ||
| 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. This violates checklist item #6: 'Do not use one-letter names'. The variable 'i' is used in loops. While this is common in Java, consider if more descriptive iteration variable names would help. |
||
| if ((element == null && elementData[i] == null) | ||
| || element != null && element.equals(elementData[i])) { | ||
| return remove(i); | ||
| } | ||
| } | ||
| throw new NoSuchElementException("There is no element founded in this List"); | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return 0; | ||
| return size; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEmpty() { | ||
|
Comment on lines
92
to
93
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. Per checklist item #4: This if-else can be simplified to |
||
| if (size() == 0) { | ||
| return true; | ||
| } | ||
| return false; | ||
|
Comment on lines
92
to
97
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 #4: 'Don't complicate if-else construction'. The if-else can be replaced with a simple return statement. |
||
| } | ||
|
|
||
| public Object[] getArrayFromCollection(List<T> list) { | ||
|
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 #6: 'Don't use abstract words like array as variable names'. Consider renaming 'arrayFromCollection' to something more informative like 'elementsFromCollection'. |
||
| Object[] arrayFromCollection = new Object[list.size()]; | ||
| for (int i = 0; i < list.size(); i++) { | ||
| arrayFromCollection[i] = list.get(i); | ||
| } | ||
| return arrayFromCollection; | ||
| } | ||
|
|
||
| public Object[] getNewCapacityArray(Object[] elementData) { | ||
|
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. Per checklist item #8: The 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. Parameter name shadows the field name |
||
| int capacityArray = elementData.length; | ||
| if (size() >= elementData.length) { | ||
| capacityArray += (capacityArray >> 1); | ||
| return Arrays.copyOf(elementData, capacityArray); | ||
|
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. Per checklist item #2: Do not use Arrays utility class. Replace |
||
| } | ||
| return elementData; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "ArrayList{" | ||
| + "elementData=" + Arrays.toString(elementData) | ||
| + ", size = " + size + '}'; | ||
| } | ||
| } | ||
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.
Magic number 10 should be a constant per checklist item #3. Create a constant like
DEFAULT_CAPACITY = 10and use it here.