Skip to content

Comments

hw#1740

Open
kaminskyivladyslav wants to merge 4 commits intomate-academy:masterfrom
kaminskyivladyslav:master
Open

hw#1740
kaminskyivladyslav wants to merge 4 commits intomate-academy:masterfrom
kaminskyivladyslav:master

Conversation

@kaminskyivladyslav
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Your implementation is solid and correctly handles the core functionality of an ArrayList. However, there are a couple of required changes related to code duplication that need to be addressed before this can be approved.

  • The logic for resizing the array is duplicated in both add methods. Please extract this into a separate private helper method to adhere to the DRY principle, as required by the task. [CHECKLIST ITEM #9]
  • Similarly, the index validation logic is repeated in several methods (get, set, remove, add). This should also be moved into a private helper method to avoid redundancy. [CHECKLIST ITEM #5]

Additionally, as a minor improvement, the isEmpty() method can be simplified to a single line: return size == 0;.

Refactoring this duplicated code is a key requirement. Once you've made these adjustments, your solution will be ready!


✨ 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

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment on lines 17 to 21
if (size == array.length) {
Object[] newArray = new Object[array.length + array.length / 2];
System.arraycopy(array, 0, newArray, 0, array.length);
array = newArray;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block of code for resizing the array is duplicated in the add(T value, int index) method (lines 28-32). This violates checklist item #9: 'Resize the array in a separate method.' You should extract this logic into a private helper method to avoid code duplication and improve maintainability.

Comment on lines 50 to 52
if (index >= size || index < 0) {
throw new ArrayListIndexOutOfBoundsException("Index out of bounds");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This index validation logic is repeated in set(T value, int index) (lines 58-60) and remove(int index) (lines 66-68). This violates checklist item #5: 'If the logic of your code repeats - move it to a separate private method.' Consider creating a private helper method to handle this check for these three methods.

Comment on lines 95 to 98
if (size == 0) {
return true;
}
return false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if-else block can be simplified. The expression size == 0 already evaluates to a boolean value. You can directly return the result of this comparison. For example: return size == 0;.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Great job on refactoring the resizing and index-checking logic into separate methods! Your code is much cleaner and well-structured now.

However, one change is needed before this can be approved. The exception messages for ArrayListIndexOutOfBoundsException should be more informative, as this is a requirement [CHECKLIST ITEM #13]. To improve debugging, please include the index that was accessed and the current size of the list in the message.

For example: throw new ArrayListIndexOutOfBoundsException("Index: " + index + ", Size: " + size);

Once you've updated the exception messages, the solution will be 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

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example


ensureCapacity();
if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException("Index out of bounds");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is informative, but it could be even more helpful for debugging. According to checklist item #13, exceptions should be informative. Consider including the invalid index and the current size of the list in the message, similar to how java.util.ArrayList does it.

For example: "Index: " + index + ", Size: " + size


private void indexOutBounds(int index) {
if (index >= size || index < 0) {
throw new ArrayListIndexOutOfBoundsException("Index out of bounds");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message is informative, but it could be even more helpful for debugging. According to checklist item #13, exceptions should be informative. Consider including the invalid index and the current size of the list in the message, similar to how java.util.ArrayList does it.

For example: "Index: " + index + ", Size: " + size

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

I'm approving your solution as an educational override so you can proceed. Great job addressing the previous feedback—your exception messages are much more informative now, and your code is well-structured!

There's just one small logical issue remaining in the remove(T element) method. It should return the element that was actually removed from the list, not the one passed as an argument. While they are equal, they might be different instances in memory.

To fix this, you can store the element from the array in a temporary variable before shifting the other elements, and then return that variable.

You've done excellent work on this task. Keep it up!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

|| array[i] == null && element == null) {
System.arraycopy(array, i + 1, array, i, size - i - 1);
size--;
return element;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should return the element that was removed from the list, not the element instance that was passed as an argument. Although they are equal (according to .equals()), they might be different instances in memory.

You should save the element from array[i] to a temporary variable before shifting the elements, and then return that variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants