Skip to content
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

fixed #1516

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

fixed #1516

wants to merge 3 commits into from

Conversation

LizaBalshv
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.

Great job on implementing the ArrayList class! 🎉 Your code meets the task requirements and constraints effectively. While there are a few areas for improvement, such as ensuring the grow() method adjusts capacity correctly and optimizing the loop in the remove method, these are minor issues that don't critically impact functionality. Keep up the good work, and consider these suggestions for future enhancements! 😊

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


int newSize = size + list.size();
if (newSize > elementData.length) {
grow();

Choose a reason for hiding this comment

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

The grow() method is called only once, which might not be sufficient if the new size is significantly larger than the current capacity. Consider using a loop to ensure the capacity is sufficient.

}

@Override
public T remove(T element) {
return null;
for (int i = 0; i < elementData.length; i++) {

Choose a reason for hiding this comment

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

The loop should iterate only up to size instead of elementData.length to avoid unnecessary checks and potential errors.

}

private void grow() {
int newCapacity = (int)(size * GROW_INDEX);

Choose a reason for hiding this comment

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

The calculation for newCapacity should ensure that it is at least one more than the current capacity to prevent an infinite loop when size is zero.

Copy link

@liliia-ponomarenko liliia-ponomarenko left a comment

Choose a reason for hiding this comment

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

Good job! Let’s improve your solution ;)

Comment on lines 39 to 42
int newSize = size + list.size();
if (newSize > elementData.length) {
grow();
}

Choose a reason for hiding this comment

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

you can remove it because the method add() handles this logic.

}

@Override
public T get(int index) {
return null;
if (index >= 0 && index < size) {

Choose a reason for hiding this comment

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

You can move the reverse condition throw an exception in a private method and reuse that method.

Comment on lines 58 to 62
if (index >= 0 && index < size) {
elementData[index] = value;
} else {
throw new ArrayListIndexOutOfBoundsException("Index: " + index + ", is invalid ");
}

Choose a reason for hiding this comment

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

avoid using if-else construction.

Suggested change
if (index >= 0 && index < size) {
elementData[index] = value;
} else {
throw new ArrayListIndexOutOfBoundsException("Index: " + index + ", is invalid ");
}
checkIndex(index);
elementData[index] = value;

And create private method:

private void checkIndex(int index) {
        if (index < 0 || index >= size) {
           throw new ArrayListIndexOutOfBoundsException("Index: " + index + ", is invalid ");
        }
    }

}

@Override
public T remove(int index) {
return null;
if (index >= 0 && index < size) {

Choose a reason for hiding this comment

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

the same. just call checkIndex method

Choose a reason for hiding this comment

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

fix comment

Choose a reason for hiding this comment

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

Use here method that you have already

Comment on lines 79 to 84
if (elementData[i] == null && element == null) {
return remove(i);
}
if (elementData[i] != null && elementData[i].equals(element)) {
return remove(i);
}

Choose a reason for hiding this comment

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

can you combine these conditionals?

}

@Override
public T remove(int index) {
return null;
if (index >= 0 && index < size) {

Choose a reason for hiding this comment

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

fix comment

Comment on lines 70 to 71
if ((elementData[i] == null && element == null)
|| (elementData[i] != null && elementData[i].equals(element))) {

Choose a reason for hiding this comment

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

Suggested change
if ((elementData[i] == null && element == null)
|| (elementData[i] != null && elementData[i].equals(element))) {
if (elementData[i] == element
|| elementData[i] != null && elementData[i].equals(element)) {

}

@Override
public T remove(int index) {
return null;
if (index >= 0 && index < size) {

Choose a reason for hiding this comment

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

Use here method that you have already

elementData[--size] = null;
return deleteValue;
}
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index);

Choose a reason for hiding this comment

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

Suggested change
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index);

Comment on lines +26 to +28
if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index);
}

Choose a reason for hiding this comment

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

you can extract this check to private method

Comment on lines +17 to +19
if (size == elementData.length) {
grow();
}

Choose a reason for hiding this comment

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

Suggested change
if (size == elementData.length) {
grow();
}
grow();

Choose a reason for hiding this comment

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

You can add this inside method grow

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.

5 participants