Skip to content

Commit

Permalink
Made corrections according to ai-mentor guidelines.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojtek-A-JAVA committed Jan 11, 2025
1 parent 34a2c7c commit 471ac13
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ArrayList<T> implements List<T> {
private int size;

public ArrayList() {
this.arrayList = (T[]) new Object[EMPTY_LIST_SIZE];
this.arrayList = (T[]) new Object[DEFAULT_CAPACITY];
this.size = EMPTY_LIST_SIZE;
}

Expand Down Expand Up @@ -45,13 +45,10 @@ public void add(T value) {

@Override
public void add(T value, int index) throws ArrayListIndexOutOfBoundsException {
if (arrayList.length == 0) {
arrayList = grow();
}
if (index > size || index < 0) {
throw new ArrayListIndexOutOfBoundsException("Index is bigger then size");
}
if (index > arrayList.length) {
if (size == arrayList.length) {
arrayList = grow();
}
T[] tempArray = (T[]) new Object[size + 1];
Expand Down Expand Up @@ -85,15 +82,15 @@ public void set(T value, int index) {
@Override
public T remove(int index) {
indexCheck(index);
T[] tempArray = (T[]) new Object[size];
size -= 1;
T[] tempArray = (T[]) new Object[size - 1];
if (index == 0 || size == 1) {
System.arraycopy(arrayList, 1, tempArray, 0, size);
System.arraycopy(arrayList, 1, tempArray, 0, size - 1);
}
if (index > 0) {
System.arraycopy(arrayList, 0, tempArray, 0, index + 1);
System.arraycopy(arrayList, index + 1, tempArray, index, size - index);
System.arraycopy(arrayList, 0, tempArray, 0, index);
System.arraycopy(arrayList, index + 1, tempArray, index, size - index - 1);
}
size -= 1;
T elementFromIndex = arrayList[index];
arrayList = tempArray;
tempArray = null;
Expand Down

0 comments on commit 471ac13

Please sign in to comment.