diff --git a/src/main/java/core/basesyntax/ArrayList.java b/src/main/java/core/basesyntax/ArrayList.java index ea82914b6..9f49348aa 100644 --- a/src/main/java/core/basesyntax/ArrayList.java +++ b/src/main/java/core/basesyntax/ArrayList.java @@ -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(); @@ -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 list) { + if (count > elements.length) { + elements = grow(); + } for (int i = 0; i < elements.length; i++) { elements[i] = list.get(i); } @@ -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++; @@ -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); } }