Skip to content

Commit

Permalink
AI feedback fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaHMK committed Dec 27, 2024
1 parent 2524623 commit 032da66
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public void add(T value, int index) {
grow();
}
Object[] result = new Object[++size];
System.arraycopy(array, 0, result, 0, index);
copy(array, 0, result, 0, index);
result[index] = value;
System.arraycopy(array, index, result, index + 1, size - index - 1);
copy(array, index, result, index + 1, size - index - 1);
array = result;
}

Expand All @@ -53,7 +53,7 @@ public void addAll(List<T> list) {

@Override
public T get(int index) {
if (isValidIndex(index)) {
if (isNotValidIndex(index)) {
throw new ArrayListIndexOutOfBoundsException("Not found "
+ "element at position: ");
} else {
Expand All @@ -63,7 +63,7 @@ public T get(int index) {

@Override
public void set(T value, int index) {
if (isValidIndex(index)) {
if (isNotValidIndex(index)) {
throw new ArrayListIndexOutOfBoundsException("Not found "
+ "element at position: " + index);
}
Expand All @@ -72,7 +72,7 @@ public void set(T value, int index) {

@Override
public T remove(int index) {
if (isValidIndex(index)) {
if (isNotValidIndex(index)) {
throw new ArrayListIndexOutOfBoundsException("Not found "
+ "element at position: " + index);
}
Expand Down Expand Up @@ -112,20 +112,26 @@ public boolean isEmpty() {

public void grow() {
Object[] newArray = new Object[array.length + array.length / 2];
System.arraycopy(array, 0, newArray, 0, size);
copy(array, 0, newArray, 0, size);
array = newArray;
}

boolean isValidIndex(int index) {
boolean isNotValidIndex(int index) {
return index < 0 || index >= size;
}

void removeAndTrim(int index) {
Object[] newArray = new Object[size--];
copy(array, 0, newArray, 0, index);
copy(array, index + 1, newArray, index, size - index);
array = newArray;
}

void copy(Object[] source, int srcIndex, Object[] destination, int destIndex, int length) {
try {
Object[] newArray = new Object[size--];
System.arraycopy(array, 0, newArray, 0, index);
System.arraycopy(array, index + 1, newArray, index, size - index);
array = newArray;
for (int i = srcIndex, j = 0; i < srcIndex + length && j < length; i++, j++) {
destination[destIndex + j] = source[i];
}
} catch (RuntimeException e) {
throw new IndexOutOfBoundsException("Index is out the array bond");
}
Expand Down

0 comments on commit 032da66

Please sign in to comment.