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

i want to take explain #1520

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

Conversation

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


Thank you for your hard work on this pull request! 😊 While you've made a great effort, there are a few critical issues that need to be addressed to ensure the functionality and reliability of the ArrayList implementation:

  1. Index Validation: The conditions in checkIndexforAdd and checkIndex methods are incorrect. For adding, the condition should allow adding at the end of the list (index <= count), and for accessing/removing, it should be index < count.

  2. Array Resizing and Shifting: The add method does not correctly handle array resizing and element shifting. This can lead to incorrect data placement and potential data loss.

  3. addAll Method: This method does not check if the current array has enough capacity to add all elements from the list, which can lead to array out-of-bounds errors.

  4. Element Removal Logic: The remove method should iterate over the elements array to find and remove the specified element, not a temporary array.

  5. Redundant and Incorrect Code: There are redundant lines and incorrect loop boundaries that need to be corrected to prevent logical errors and improve code clarity.

  6. Use of Utility Classes: The use of Arrays.copyOf() violates the constraint of not using utility classes, which is critical to address.

Please address these issues to ensure the code meets the core requirements and functions as expected. Once these changes are made, I look forward to approving your improved solution! Keep up the great work, and don't hesitate to reach out if you need any help. 🚀

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

🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review button in the Reviewers tab.


}

@Override
public void add(T value, int index) {
checkIndexforAdd(index);
count++;
Object[] newArray = new Object[MAX_CAPACITY];

Choose a reason for hiding this comment

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

The newArray should be initialized with the current size of elements plus one, not MAX_CAPACITY. This ensures that the array can accommodate the new element.

Comment on lines +29 to +34
for (int i = 0; i < newArray.length; i++) {
if (i == index) {
break;
}
newArray[i] = elements[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 count, not newArray.length, to avoid accessing uninitialized elements.

Comment on lines +36 to +38
for (int i = index + 1; i < newArray.length; i++) {
newArray[i] = elements[i - 1];
}

Choose a reason for hiding this comment

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

The loop should iterate from index to count, not newArray.length, to correctly shift elements after the insertion point.

newArray[i] = elements[i - 1];
}
elements = newArray;
elements[index] = value;

Choose a reason for hiding this comment

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

This line is redundant because newArray[index] = value; has already been set. It should be removed.

Comment on lines +47 to +49
for (int i = 0; i < elements.length; i++) {
elements[i] = list.get(i);
}

Choose a reason for hiding this comment

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

The addAll method should check if the current array has enough capacity to add all elements from the list. If not, it should grow the array accordingly.

Comment on lines 90 to 99
for (int i = 0; i < newArray1.length; i++) {
if (newArray1[i] == element || (element != null
&& element.equals(newArray1[i]))) {
a = i;
result = element;
thisExist++;
break;
}
newArray1[i] = elements[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 over elements, not newArray1, to find the element to remove. This will ensure that the correct element is found and removed.

}

private void checkIndexforAdd(int index) {
if (index < 0 || index >= count) {

Choose a reason for hiding this comment

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

The condition should be index < 0 || index > count to allow adding at the end of the list (i.e., at count).

}

private void checkIndex(int index) {
if (index < 0 || index > count) {

Choose a reason for hiding this comment

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

The condition should be index < 0 || index >= count to correctly validate existing indices.

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.

Sorry, the pull request review was unsuccessful for the following reasons:

Warning

No changes were made in the pull request since the last review. Please make sure that you have added files to the pull request and committed the changes.
[CODE: 7]

If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.

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 effort on implementing the ArrayList! 🎉 However, there are several critical issues that need attention to ensure the functionality and adherence to constraints:

  1. Use of Utility Classes: The grow method uses Arrays.copyOf(), which violates the constraint of not using utility classes. Implement the array copying manually.

  2. Array Resizing and Shifting: The add method needs to correctly handle array resizing and element shifting. Ensure the logic for growing the array and shifting elements is properly separated and implemented.

  3. addAll Method: Ensure the current array has enough capacity to add all elements from the list by resizing appropriately before adding elements.

  4. Element Removal Logic: The remove method should not create a new array each time an element is removed. Instead, shift elements within the existing array to maintain efficiency.

  5. Index Validation: The checkIndexforAdd method should allow adding at the end of the list, and the set method should use checkIndex instead.

  6. Loop Boundaries: Correct the loop boundaries in methods like addAll and grow to ensure proper functionality.

  7. Informative Exception Messages: Include informative messages in exceptions to clarify what went wrong.

These issues are crucial for the correct functionality of your ArrayList. Addressing them will greatly improve your code. Keep up the good work, and use this opportunity to learn and enhance your coding skills! 🚀

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

@@ -1,48 +1,139 @@
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.

The use of Arrays.copyOf() in the grow method violates the constraint of not using utility classes. You should implement array copying manually.


}

@Override
public void add(T value, int index) {
checkIndexforAdd(index);

Choose a reason for hiding this comment

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

The checkIndexforAdd method should allow adding at the end of the list, which means the condition should be index <= count.

checkIndexforAdd(index);
count++;
Object[] newArray = new Object[elements.length + 1];
if (count > MAX_CAPACITY) {

Choose a reason for hiding this comment

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

The condition count > MAX_CAPACITY is incorrect for determining when to grow the array. It should be count >= elements.length.

newArray = grow();

}
for (int i = 0; i < newArray.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 over elements.length instead of newArray.length to correctly copy elements up to the specified index.


}

@Override
public void addAll(List<T> list) {
if (count > elements.length) {

Choose a reason for hiding this comment

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

The condition count > elements.length is incorrect for determining when to grow the array. It should be count + list.size() > elements.length to ensure there is enough space for all elements.

if (count > elements.length) {
elements = grow();
}
for (int i = 0; i < elements.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 over list.size() instead of elements.length to correctly add all elements from the list.

}

@Override
public void set(T value, int index) {

checkIndexforAdd(index);

Choose a reason for hiding this comment

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

The checkIndexforAdd method is incorrectly used here. The set method should use checkIndex instead, as it is not adding a new element but replacing an existing one.

return null;
checkIndex(index);
T element = (T) elements[index];
Object[] newArray1 = new Object[elements.length - 1];

Choose a reason for hiding this comment

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

Creating a new array with elements.length - 1 is incorrect. The array should remain the same size, and elements should be shifted to fill the gap left by the removed element.

}

@Override
public T remove(T element) {
return null;
T result = null;
Object[] newArray1 = new Object[elements.length - 1];

Choose a reason for hiding this comment

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

Similar to the remove(int index) method, creating a new array with elements.length - 1 is incorrect. The array should remain the same size, and elements should be shifted to fill the gap left by the removed element.

}

public Object[] grow() {
return Arrays.copyOf(elements, MAX_CAPACITY + (MAX_CAPACITY >> 1));

Choose a reason for hiding this comment

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

The grow method should not use Arrays.copyOf(). Implement the array copying manually to adhere to the constraint of not using utility classes.

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