Skip to content

Comments

created simplified custom array-list#1742

Open
3xactCoder wants to merge 4 commits intomate-academy:masterfrom
3xactCoder:array-list-task
Open

created simplified custom array-list#1742
3xactCoder wants to merge 4 commits intomate-academy:masterfrom
3xactCoder:array-list-task

Conversation

@3xactCoder
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

Great work on your ArrayList implementation! You've correctly handled most of the core logic, including capacity management, index checks, and using System.arraycopy for element shifting. However, this requires changes because the use of Arrays.copyOf() is not allowed for this task, as it violates a key requirement [CHECKLIST ITEM #2]. The goal is to implement this functionality yourself. To fix this, please replace Arrays.copyOf() in your resizing method. You should manually create a new array with the calculated new size and then use System.arraycopy() to copy the elements from the old array into the new one. Once you've made that change, your solution will be in great shape!


✨ 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

@@ -1,48 +1,103 @@
package core.basesyntax;

import java.util.Arrays;

Choose a reason for hiding this comment

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

This violates checklist item #2: 'Don't use Objects, Arrays, or any other util class.' This import should be removed, and the functionality from this class should be implemented manually.

public void checkCapacity() {
if (count == elements.length) {
int newSize = elements.length + elements.length / 2;
elements = Arrays.copyOf(elements,newSize);

Choose a reason for hiding this comment

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

This violates checklist item #2: 'Don't use Objects, Arrays, or any other util class.' Instead of using Arrays.copyOf(), you should create a new array of newSize and then use System.arraycopy() to copy the elements from the old elements array to the new one.

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 the progress so far, especially with replacing Arrays.copyOf! Your solution is very close. However, changes are needed to fix two remaining issues.

First, the addAll method will cause an infinite loop if a list is added to itself (e.g., myList.addAll(myList)). This is because the loop condition list.size() is re-evaluated after each add operation, which increases the size. A good way to solve this is to copy the elements from the incoming collection into a separate array before you start adding them.

Second, the array growth logic in your checkCapacity method uses the magic number 2. Please extract this into a named constant to comply with the checklist requirement [CHECKLIST ITEM #3].

After you fix these two points, your solution will be in great shape!


✨ 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

@Override
public void addAll(List<T> list) {

for (int i = 0;i < list.size();i++) {

Choose a reason for hiding this comment

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

The current implementation of addAll will cause an infinite loop if the list is added to itself (e.g., myList.addAll(myList)). This is because list.size() is re-evaluated on each loop iteration, but add() increases the size of the list. To meet the requirement that your implementation behaves like java.util.ArrayList, this case needs to be handled correctly.


public void checkCapacity() {
if (count == elements.length) {
int newSize = elements.length + elements.length / 2;

Choose a reason for hiding this comment

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

This line uses the magic number 2 for the growth calculation. This violates checklist item #3: 'Any magic numbers should be constants'. Please extract this value into a named constant, for example GROWTH_DIVISOR, to improve readability and maintainability.

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

Excellent work on the fixes! I am approving this solution as an educational override to help you move forward, but please review the following points for your next tasks. You did a great job fixing the addAll method and extracting the magic number.

Here are a few areas for improvement:

  • Naming: The constant MAGIC_NUM is not very descriptive. A name like GROWTH_RATE_DIVISOR would better explain its purpose. [CHECKLIST ITEM #6]
  • Encapsulation: Helper methods like checkIndex and checkCapacity should be private to hide the internal implementation details of the class.
  • Growth Logic: There's a subtle bug in the growth calculation. If the array size is 1, 1 + 1 / 2 results in 1 due to integer division, which prevents the array from growing. The new size must always be at least current size + 1.

These are common refinements in programming. Keep up the great effort!


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

Footnotes

  1. Rate AI review example


public class ArrayList<T> implements List<T> {
private static final int DEFAULT_SIZE = 10;
private static final int MAGIC_NUM = 2;

Choose a reason for hiding this comment

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

This is a great improvement to extract the magic number 2. However, MAGIC_NUM is not a very informative name. According to checklist item #6 ('Use informative names for your variables and methods'), a name should make it clear what the variable is responsible for. A name like GROWTH_RATE_DIVISOR or CAPACITY_GROWTH_DIVIDER would better describe its purpose in the capacity calculation.

Comment on lines +95 to +101
public void checkIndex(int index) {
if (index < 0 || index >= count) {
throw new ArrayListIndexOutOfBoundsException("Index is incorrect");
}
}

public void checkCapacity() {

Choose a reason for hiding this comment

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

These helper methods (checkIndex and checkCapacity) are part of the internal implementation of your ArrayList. To properly encapsulate the class's logic and prevent them from being called from outside the class, they should be declared as private.

}
}

public void checkCapacity() {

Choose a reason for hiding this comment

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

The checklist (item #6) provides a good example regarding method names that contain conditional logic. Since this method only grows the array if it's full, a more descriptive name would be growIfFull() or ensureCapacityIsSufficient(). This makes it clearer to the reader what the method does.


public void checkCapacity() {
if (count == elements.length) {
int newSize = elements.length + elements.length / MAGIC_NUM;

Choose a reason for hiding this comment

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

There's a subtle issue with this growth calculation for small array sizes. If elements.length is 1, newSize will also be 1 due to integer division (1 + 1 / 2 = 1 + 0 = 1). This means the array won't grow, which will lead to an ArrayIndexOutOfBoundsException on the next add operation. The new size must always be at least current size + 1.

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