-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: master
Are you sure you want to change the base?
fixed #1516
Conversation
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.
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(); |
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.
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++) { |
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.
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); |
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.
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.
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.
Good job! Let’s improve your solution ;)
int newSize = size + list.size(); | ||
if (newSize > elementData.length) { | ||
grow(); | ||
} |
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.
you can remove it because the method add()
handles this logic.
} | ||
|
||
@Override | ||
public T get(int index) { | ||
return null; | ||
if (index >= 0 && index < size) { |
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.
You can move the reverse condition throw an exception in a private method and reuse that method.
if (index >= 0 && index < size) { | ||
elementData[index] = value; | ||
} else { | ||
throw new ArrayListIndexOutOfBoundsException("Index: " + index + ", is invalid "); | ||
} |
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.
avoid using if-else construction.
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) { |
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.
the same. just call checkIndex method
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.
fix comment
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.
Use here method that you have already
if (elementData[i] == null && element == null) { | ||
return remove(i); | ||
} | ||
if (elementData[i] != null && elementData[i].equals(element)) { | ||
return remove(i); | ||
} |
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.
can you combine these conditionals?
} | ||
|
||
@Override | ||
public T remove(int index) { | ||
return null; | ||
if (index >= 0 && index < size) { |
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.
fix comment
if ((elementData[i] == null && element == null) | ||
|| (elementData[i] != null && elementData[i].equals(element))) { |
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.
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) { |
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.
Use here method that you have already
elementData[--size] = null; | ||
return deleteValue; | ||
} | ||
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index); |
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.
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index); |
if (index < 0 || index > size) { | ||
throw new ArrayListIndexOutOfBoundsException("Invalid index: " + index); | ||
} |
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.
you can extract this check to private method
if (size == elementData.length) { | ||
grow(); | ||
} |
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.
if (size == elementData.length) { | |
grow(); | |
} | |
grow(); |
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.
You can add this inside method grow
No description provided.