public class PartialArrayListOfInt implements ListOfInt {
    protected int limit;      // maximal number of elements
    protected int[] data;     // elements of the list
    protected int num = 0;    // current number of elements
    public PartialArrayListOfInt(int limit) {
        this.limit = limit;
        this.data = new int[limit];
    }
    public int get(int i) {
        if (i < 0 || i >= num) {
            throw new IndexOutOfBoundsException();
        }
        return this.data[i];
    }
    public void set(int i, int elem) {
        if (i < 0 || i >= num) {
            throw new IndexOutOfBoundsException();
        }
        this.data[i] = elem;
    }
    public int size() {
        return this.num;
    }
    public void add(int elem) {
        this.add(this.num, elem);       // insert at end
    }
    public void add(int i, int elem) {
        if (i < 0 || i > num) {  throw new IndexOutOfBoundsException();  }
        if (num >= limit) {  throw new RuntimeException("full!");  }
        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!
    }
    public void remove(int i) {
        if (i < 0 || i >= num) {  throw new IndexOutOfBoundsException();  }
       for (int j = i; j+1 < num; j++) {
           this.data[j] = this.data[j+1];  // move elements left
       }
       num--;              // one element less!
   }
   // DONE!
}
