Dynamic array iterator

Report a typo

In this task, you must write an iterator for a custom data structure representing an array of variable length (a so-called dynamic array). The iterator you will write must implement the provided ArrayIterator<T> interface. Also, don't forget to add and complete the public ArrayIterator<T> iterator() method.

The DynamicArray<T> has a bunch of methods and may be created with an arbitrary initial capacity. It will initially hold the specified number of null elements. Note that the size of DinamicArray changes if an element is appended to or removed from it.

Write a program in Java 17
import java.util.*;

interface ArrayIterator<T> {

boolean hasNext();

T next();
}

class DynamicArray<T> {
private static final float INFLATION_FACTOR = 1.5f;

private T[] array;
private int size;

public DynamicArray(int initialSize) {
array = (T[]) new Object[initialSize];
size = initialSize;
}

public void insertAt(int index, T value) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(
"Index " + index + " out of bound for length " + size);
}
array[index] = value;
}

public void append(T value) {
if (size == array.length) {
inflate();
}
array[size++] = value;
}

public int indexOf(T value) {
___

Create a free account to access the full topic