package arrays; import java.util.Arrays; public class MyDynamicArray { private int array[]; private int size; private int capacity; /** * Constructor with initial capacity of 2 */ public MyDynamicArray(){ array = new int[2]; size=0; capacity=2; } /** * check for capacity and if there is capacity add the element at the end of the array. * @param element */ public void addElement(int element){ if (size == capacity){ try{ increaseCapacity(2); }catch(Exception e){ System.out.println("unable to increase the capacity of the array"); e.printStackTrace(); } } array[size++] = element; } /** * Adding element at a given index * @param index * @param element */ public void addElement(int index, int element){ //check if the array has capacity to add element. if (size == capacity){ try{ increaseCapacity(2); }catch(Exception e){ System.out.println("unable to increase the capacity of the array"); e.printStackTrace(); } } // shift all elements from the given index to right for(int i=size-1;i>=index;i--){ array[i+1] = array[i]; } // insert the element at the specified index array[index] = element; size++; } /** * get an element from given index, O(1) time complexity * @param index * @return */ public int getElement(int index){ return array[index]; } /** * Remove an element at the last index */ public void remove(){ if(size>1){ array[size--]=0; } } /** * Remove an element from given index * @param index */ public void removeAt(int index){ if(index>=size || index<0){ System.out.println("No element at this index"); }else{//shifting for(int i=index;i