public class DynamicArray { Object[] store; int capacity; // how many items can store int size; // how many items stored public DynamicArray() { store = new Object[10]; capacity = 10; size = 0; } public void add(Object o) { // if the store has space, insert o if (size < capacity) { store[size] = o; size++; } else { capacity = capacity * 2; // or whatever Object[] newstore = new Object[capacity]; for (int i = 0; i < size; i++) { newstore[i] = store[i]; } store = newstore; store[size] = o; size++; } } public Object get(int i) { // check if i is within bounds; if ((0 <= i) && (i < size)) return store[i]; else throw new ArrayIndexOutOfBoundsException(); } public boolean remove(Object o) { int index = -1; // look for o in store for (int i = 0; i < size; i++) { if(store[i].equals(o)) { index = i; break; } } // if o not in store, return false if (index == -1) return false; // replace o by the next value and shift left for (int i = index; i < size-1; i++) { store[i] = store[i+1]; } size--; return true; } public ArrayIterator iterator() { return new ArrayIterator(this); } public static void main(String[] args) { DynamicArray myarray = new DynamicArray(); String[] numbers = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; for (int i = 0; i < 10; i++) { myarray.add(numbers[i]); } ArrayIterator it = myarray.iterator(); while(it.hasNext()){ System.out.print(it.next() + " "); } System.out.println(); } class ArrayIterator { DynamicArray array; int current; ArrayIterator(DynamicArray d) { array = d; current = 0; } Object next() { return array.get(current++); } boolean hasNext() { return (current < array.size); } } }