Skip to content

Commit

Permalink
1. Динамический список на массиве. [#158 #179646]
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzor committed Jul 2, 2024
1 parent a66bf45 commit 08f88f9
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/main/java/ru/j4j/collection/SimpleArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.*;

public class SimpleArrayList<T> implements SimpleList<T> {

private T[] container;
private int size;
private int modCount;
Expand All @@ -12,8 +13,8 @@ public SimpleArrayList(int capacity) {
}

private void grow() {
int newLength = container.length > 0 ? container.length * 2 : 1;
container = Arrays.copyOf(container, newLength);
int newCapacity = container.length > 0 ? container.length * 2 : 1;
container = Arrays.copyOf(container, newCapacity);
}

@Override
Expand All @@ -25,6 +26,13 @@ public void add(T value) {
modCount++;
}

@Override
public T set(int index, T newValue) {
T changedValue = get(index);
container[index] = newValue;
return changedValue;
}

@Override
public T remove(int index) {
T removedValue = get(index);
Expand All @@ -34,13 +42,6 @@ public T remove(int index) {
return removedValue;
}

@Override
public T set(int index, T newValue) {
T changedValue = get(index);
container[index] = newValue;
return changedValue;
}

@Override
public T get(int index) {
Objects.checkIndex(index, size);
Expand All @@ -55,12 +56,12 @@ public int size() {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private int index = 0;
public final int expectedModCount = modCount;
int index = 0;
final int expectedModCount = modCount;

@Override
public boolean hasNext() {
if (modCount != expectedModCount) {
if (expectedModCount != modCount) {
throw new ConcurrentModificationException("cannot be changed during iteration");
}
return index < size;
Expand Down

0 comments on commit 08f88f9

Please sign in to comment.