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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 97 additions & 6 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -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.

import java.util.NoSuchElementException;

public class ArrayList<T> implements List<T> {
private int count;
public static final int MAX_CAPACITY = 10;
private Object[] elements = new Object[MAX_CAPACITY];
@Override
public void add(T value) {
if (count >= elements.length) {
elements = grow();
}
elements[count] = value;
count++;

}

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

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.

if (i == index) {
break;
}
newArray[i] = elements[i];
}
Comment on lines +29 to +34

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.

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

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.

elements = newArray;

}

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

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.

elements[i] = list.get(i);
}
Comment on lines +48 to +50

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.


}

@Override
public T get(int index) {
return null;
checkIndex(index);
return (T) elements[index];
}

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

elements[index] = value;
}

@Override
public T remove(int index) {
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.

for (int i = 0; i < newArray1.length; i++) {
if (i == index) {
break;
}
newArray1[i] = elements[i];
}
for (int i = index; i < newArray1.length; i++) {
newArray1[i] = elements[i + 1];
}
count--;
elements = newArray1;
return 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.

int a = 0;
int thisExist = 0;
for (int i = 0; i < elements.length; i++) {
if (elements[i] == element || (element != null
&& element.equals(elements[i]))) {
a = i;
result = element;
thisExist++;
break;
}
newArray1[i] = elements[i];
}
if (thisExist == 0) {
throw new NoSuchElementException();
}

for (int i = a; i < newArray1.length; i++) {
newArray1[i] = elements[i + 1];
}
count--;
elements = newArray1;
return result;
}

@Override
public int size() {
return 0;
return count;
}

@Override
public boolean isEmpty() {
return false;
return count == 0;
}

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.

}

private void checkIndexforAdd(int index) {
if (index < 0 || index > count) {
throw new ArrayListIndexOutOfBoundsException("Incorrect index: " + index);
}

}

private void checkIndex(int index) {
if (index < 0 || index >= count) {
throw new ArrayListIndexOutOfBoundsException("Incorrect index: " + index);
}
}
}
Loading