Conversation
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
This is a great effort, and you've correctly implemented many core features like the custom exception and the use of System.arraycopy(). However, a couple of critical issues need to be addressed before this can be approved.
There's a significant bug in your addAll() method that causes an infinite loop. This happens because your grow() method only resizes the array when it's completely full (size == elementData.length). If addAll() needs more space but the array isn't at maximum capacity, grow() does nothing, and the loop never ends. To fix this, grow() should only handle the resizing logic, and the check for when to grow should be moved into the methods that add elements. This also resolves the misleading method name [CHECKLIST ITEM #6].
Additionally, the code for shifting elements after a removal is duplicated in both remove(int index) and remove(T element). Please extract this logic into a private helper method to avoid repetition [CHECKLIST ITEM #5].
Fixing these issues will make your implementation robust and complete. Keep up the great work!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
| size = 0; | ||
| } | ||
|
|
||
| private void grow() { |
There was a problem hiding this comment.
This violates checklist item #6: 'Use informative names for your variables and methods.' The method name grow suggests it always increases the array's capacity, but it only does so conditionally when size == elementData.length. This can be misleading and, as seen in addAll(), can cause bugs. Consider moving the capacity check to the methods that call grow and have this method be solely responsible for the resizing logic.
| } | ||
|
|
||
| int additionalSize = list.size(); | ||
| while (size + additionalSize > elementData.length) { |
There was a problem hiding this comment.
This while loop can result in an infinite loop. If the array is not completely full but doesn't have enough space for the new elements (e.g., size=8, capacity=10, list.size()=3), the condition size + additionalSize > elementData.length will be true. However, the grow() method will not resize the array because size is not equal to elementData.length. The loop will continue indefinitely without changing state. The capacity should be checked and the array resized once before adding the new elements, not within a loop.
| T removed = elementData[i]; | ||
| int numMoved = size - i - 1; | ||
|
|
||
| if (numMoved > 0) { | ||
| System.arraycopy(elementData, i + 1, elementData, i, numMoved); | ||
| } | ||
|
|
||
| elementData[--size] = null; | ||
| return removed; |
There was a problem hiding this comment.
This block of code is a duplication of the logic inside the remove(int index) method. This violates checklist item #5: 'Don't create repeating code.' To fix this, you can call remove(i) after finding the index i of the element, or extract the common removal logic into a private helper method that both remove methods can use.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Excellent work on this iteration! You've successfully addressed the critical bugs from the previous review, and the code is much cleaner. This requires just one final adjustment to be perfect. The capacity check logic (if (size == elementData.length)) is duplicated in both add() methods in ArrayList.java. Please extract this check into a new private helper method to avoid repetition, as per checklist item #5 ('Don't create repeating code'). Also, please remove the minor redundant empty line in ArrayListIndexOutOfBoundsException.java. After these small fixes, your solution will be ready for approval. Keep up the great work!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Important
Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.
Tip
If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.
Caution
Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.
Footnotes
| public ArrayListIndexOutOfBoundsException(String message) { | ||
| super(message); | ||
| } | ||
|
|
There was a problem hiding this comment.
This violates checklist item #1: 'Remove all redundant empty lines'. Please remove this empty line between the two constructors.
| if (size == elementData.length) { | ||
| grow(); |
There was a problem hiding this comment.
This capacity check logic is also present in the add(T value, int index) method (line 69). This violates checklist item #5: 'Don't create repeating code'. Consider extracting this check into a new private helper method to avoid duplication.
mateacademy-ai-mentor
left a comment
There was a problem hiding this comment.
Review Summary
Excellent work on this iteration! You've successfully refactored the capacity-checking logic into a helper method, which has significantly improved the code's structure and readability.
I am approving your solution as an educational exception so you can proceed. Please note there is one minor code style issue left in ArrayListIndexOutOfBoundsException.java: the empty line before the class declaration. In future tasks, please remember to remove such redundant lines to adhere to the project's style guide [CHECKLIST ITEM #1].
Overall, you've made great progress. Keep up the fantastic work!
✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨
Footnotes
| package core.basesyntax; | ||
|
|
||
| public class ArrayListIndexOutOfBoundsException extends RuntimeException { | ||
| public class ArrayListIndexOutOfBoundsException extends IndexOutOfBoundsException { |
There was a problem hiding this comment.
This violates checklist item #1: 'Don't begin class or method implementation with an empty line'. Please remove the empty line before this class declaration.
No description provided.