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

Relised list #1531

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

Conversation

Tarasmarkov301
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 implementation! However, there are a few critical issues that need to be addressed to ensure the ArrayList functions correctly. The initial size and capacity management are incorrect, which affects several methods like add, addAll, get, set, and remove. These issues impact the core functionality of the list, such as incorrect size calculations and index validations. Please address these to meet the task requirements. Once resolved, your code will be much stronger! Keep up the great effort! 😊

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

public class ArrayList<T> implements List<T> {
private Object[] array = new Object[10];
private int size = 1;

Choose a reason for hiding this comment

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

The initial size is set to 1, which is incorrect. It should be initialized to 0 since the list is empty initially.

System.arraycopy(array, 0, newArray, 0, array.length);
array = newArray;
}
array[size - 1] = value;

Choose a reason for hiding this comment

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

The assignment array[size - 1] = value; is incorrect. It should be array[size] = value; since size should represent the number of elements in the list, not the next available index.

System.arraycopy(array, 0, newArray, 0, array.length);
array = newArray;
for (int i = 0; i < list.size(); i++) {
array[i + size - 1] = list.get(i);

Choose a reason for hiding this comment

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

The assignment array[i + size - 1] = list.get(i); is incorrect. It should be array[i + size] = list.get(i); to correctly place elements from the list being added.

for (int i = 0; i < list.size(); i++) {
array[i + size - 1] = list.get(i);
}
size = tempSize + 1;

Choose a reason for hiding this comment

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

The size update size = tempSize + 1; is incorrect. It should be size = tempSize; since tempSize already accounts for the total number of elements after adding the list.

public T get(int index) {
return null;
public T get(int index) throws ArrayListIndexOutOfBoundsException {
if (index >= size - 1 || index < 0) {

Choose a reason for hiding this comment

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

The condition index >= size - 1 is incorrect. It should be index >= size to correctly check if the index is within bounds.

public void set(T value, int index) {

public void set(T value, int index) throws ArrayListIndexOutOfBoundsException {
if (index >= size - 1 || index < 0) {

Choose a reason for hiding this comment

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

The condition index >= size - 1 is incorrect. It should be index >= size to correctly check if the index is within bounds.

}

@Override
public int size() {
return 0;
return size - 1;

Choose a reason for hiding this comment

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

The return statement return size - 1; is incorrect. It should be return size; since size should represent the number of elements in the list.

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 your ArrayList implementation! 🎉 While there are several critical issues that need attention, such as the resizing logic in the add and addAll methods and the element shifting in the remove method, your effort is commendable. It's important to address these issues to ensure the ArrayList functions correctly.

Here are some key points to focus on:

  • Resizing Logic: Ensure that the array resizes correctly when full. The condition should be size >= array.length to trigger resizing at the right time. In the addAll method, verify that the array can accommodate all new elements and resize accordingly.

  • Element Shifting: In the remove method, use size - index - 1 in System.arraycopy to correctly shift elements and avoid out-of-bounds errors.

  • Code Optimization: Extract the resizing logic into a separate method to adhere to the DRY principle, and avoid using utility classes like Objects.equals().

  • Magic Numbers and Constants: Move hardcoded values like the initial capacity to a constant field for better readability and maintainability.

Remember, addressing these issues will not only improve your code but also enhance your understanding of data structures. Keep up the good work, and continue learning from these experiences! 🚀

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

@Override
public void add(T value) {

if (size + 1 == array.length) {

Choose a reason for hiding this comment

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

The condition size + 1 == array.length should be size == array.length to ensure resizing occurs when the array is full, not when it's about to be full.

if (index > size() || index < 0) {
throw new ArrayListIndexOutOfBoundsException("Index out of bounds");
}
if (size + 1 >= array.length) {

Choose a reason for hiding this comment

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

The condition size + 1 >= array.length should be size >= array.length to ensure resizing occurs when the array is full.

Comment on lines 38 to 43
int summarySize = list.size() + size();
final int tempSize = summarySize;
summarySize = summarySize + summarySize / 2;
Object[] newArray = new Object[summarySize];
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.

The resizing logic in addAll is incorrect. You should check if the current array can accommodate the new elements and resize accordingly. The calculation of summarySize is also incorrect; it should ensure the new array size is sufficient to hold all elements.

throw new ArrayListIndexOutOfBoundsException("Index out of bounds");
}
T result = (T) array[index];
System.arraycopy(array, index + 1, array, index, array.length - index - 1);

Choose a reason for hiding this comment

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

The System.arraycopy call in remove should use size - index - 1 instead of array.length - index - 1 to correctly shift elements and avoid accessing out of bounds.

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.

3 participants