Skip to content

Commit

Permalink
i want to take explain
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasha1256 committed Nov 28, 2024
1 parent 7cb19e9 commit d93d7bc
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void add(T value) {
public void add(T value, int index) {
checkIndexforAdd(index);
count++;
Object[] newArray = new Object[MAX_CAPACITY];
Object[] newArray = new Object[elements.length + 1];
if (count > MAX_CAPACITY) {
newArray = grow();

Expand All @@ -37,13 +37,14 @@ public void add(T value, int index) {
newArray[i] = elements[i - 1];
}
elements = newArray;
elements[index] = value;


}

@Override
public void addAll(List<T> list) {
if (count > elements.length) {
elements = grow();
}
for (int i = 0; i < elements.length; i++) {
elements[i] = list.get(i);
}
Expand Down Expand Up @@ -87,9 +88,9 @@ public T remove(T element) {
Object[] newArray1 = new Object[elements.length - 1];
int a = 0;
int thisExist = 0;
for (int i = 0; i < newArray1.length; i++) {
if (newArray1[i] == element || (element != null
&& element.equals(newArray1[i]))) {
for (int i = 0; i < elements.length; i++) {
if (elements[i] == element || (element != null
&& element.equals(elements[i]))) {
a = i;
result = element;
thisExist++;
Expand Down Expand Up @@ -124,14 +125,14 @@ public Object[] grow() {
}

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

}

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

0 comments on commit d93d7bc

Please sign in to comment.