public class DynamicArrayListOfInt extends PartialArrayListOfInt {
    public DynamicArrayListOfInt(int limit) {
        super(limit);
    }
    public void add(int i, int elem) {
        if (i < 0 || i > num) {  throw new IndexOutOfBoundsException();  }
        if (num >= limit) { // array is full
            int[] newData = new int[2*this.limit];
            for (int j = 0; j < limit; j++) {
                newData[j] = data[j];
            }
            this.data = newData;
            this.limit *= 2;
        }
        for (int j = num-1; j >= i; j--) {
            this.data[j+1] = this.data[j];  // move elements right
        }
        this.data[i] = elem;        // insert new element
        num++;              // one element more!
    }
}