-
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 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,130 @@ | ||
| package core.basesyntax; | ||
|
|
||
| import java.util.NoSuchElementException; | ||
|
|
||
| public class ArrayList<T> implements List<T> { | ||
| private static final int DEFAULT_ARRAY_CAPACITY = 10; | ||
| private Object[] elementData = new Object[DEFAULT_ARRAY_CAPACITY]; | ||
| private int size; | ||
|
|
||
| @Override | ||
| public void add(T value) { | ||
|
|
||
| elementData = checkCapacity(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 = checkCapacity(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 = checkCapacity(summaryArray); | ||
| size += list.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public T get(int index) { | ||
| return null; | ||
| boolean rightIndex = validateIndex(index); | ||
| if (rightIndex == true) { | ||
| 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. |
||
|
|
||
| boolean rightIndex = validateIndex(index); | ||
| if (rightIndex == true) { | ||
| 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; | ||
| boolean rigthIndex = validateIndex(index); | ||
| if (rigthIndex == true) { | ||
| 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[] checkCapacity(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. This violates checklist item #7: These helper methods (checkCapacity, validateIndex, getArrayFromCollection, getNewCapacityArray) are not part of the List interface and should be private, not public. |
||
| if (size() >= elementData.length) { | ||
| elementData = getNewCapacityArray(elementData); | ||
| return elementData; | ||
| } | ||
| return elementData; | ||
|
Comment on lines
+100
to
+105
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 checkCapacity method creates redundant assignments. Since the method returns a value either way, you can simply return the result directly without the intermediate variable assignment. |
||
| } | ||
|
|
||
| public boolean validateIndex(int index) { | ||
| if (index < size() && index >= 0) /*&& elementData[index] != null)*/ { | ||
| return true; | ||
| } | ||
| return false; | ||
|
Comment on lines
+108
to
+112
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 validateIndex() method should throw the exception directly instead of returning boolean, eliminating the if-else pattern in calling methods. Compare to the 'Bad example' in the checklist. |
||
| } | ||
|
|
||
| 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; | ||
| capacityArray += (capacityArray >> 1); | ||
| Object[] newArray = new Object[capacityArray]; | ||
| System.arraycopy(elementData, 0, newArray, 0, size()); | ||
| return newArray; | ||
| } | ||
| } | ||
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.
Per checklist item #5: The index validation logic
index < size() && index >= 0is repeated inget(),set(), andremove(int index)methods. Move this to a private helper method likevalidateIndex(int index).