import java.util.Arrays;

public class DynArray {
	// Instance variables
	private int[] data;
	
	public DynArray() {
		data = new int[0];
	}
	
	// Get a value
	public int getElement(int index) {
		if (index < 0 || index >= data.length)
			throw new IndexOutOfBoundsException("Dynamic Array index out of bounds");
		return data[index];
	}

	public void setElement(int index, int newValue) {
		if (index < 0 || index >= data.length)
			throw new IndexOutOfBoundsException("Dynamic Array index out of bounds");
		data[index] = newValue;
	}
	
	public void insertValue(int insertIndex, int newValue) {
		// make a new larger array
		int[] newData = new int[data.length + 1];
		// Copy values below insertIndex
		for (int index = 0; index < insertIndex; index++) {
			newData[index] = data[index];
		}
		// Add the value at insertIndex
		newData[insertIndex] = newValue;
		// Copy and shift values above insertIndex
		for (int index = insertIndex; index < data.length; index++) {
			newData[index+1] = data[index];
		}
		// Assign the new array to the instance variable
		data = newData;
	}
	
	public String toString() {
		return Arrays.toString(data);
	}

	public static void main(String[] args) {
		
		DynArray vals = new DynArray();
		vals.insertValue(0, 10);
		System.out.println(vals);
		vals.insertValue(1, 20);
		System.out.println(vals);
		vals.insertValue(0, 30);
		System.out.println(vals);

	}

}
