Skip to content

Commit

Permalink
attempt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dantesworld committed Dec 26, 2024
1 parent a837ba9 commit 8665480
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

public class ArrayList<T> implements List<T> {

private static final int defaultSize = 10;
private Object[] arrayList = new Object[defaultSize];
private static final int DEFAULT_SIZE = 10;
private Object[] arrayList = new Object[DEFAULT_SIZE];
private int size = 0;

private void grow() {
Expand All @@ -32,9 +32,7 @@ public void add(T value, int index) {
}
Object[] newList = new Object[arrayList.length];

for (int i = 0; i < index; i++) {
newList[i] = arrayList[i];
}
System.arraycopy(arrayList, 0, newList, 0, index);
newList[index] = value;
for (int i = index; i < size(); i++) {
newList[i + 1] = arrayList[i];
Expand All @@ -45,6 +43,7 @@ public void add(T value, int index) {

@Override
public void addAll(List<T> list) {

if (arrayList.length < size() + list.size()) {
grow();
}
Expand Down

0 comments on commit 8665480

Please sign in to comment.