I tried to make method which inserts element at the specified position in this list. Then Shifts the element & subsequent elements currently at that position to the Right by adding one to their indices, i know there is shortcut for this method but I am suppose to do it, here what i tried to do but it's not working.
private T a[]; private int count; private int size = 0; public int size() { return size; } public void add(int index,T t) throws Exception { if (index < 0 || index > = a.length){ throw new IndexOutOfBoundsException(); } Object[] temp = new Object[a.length + 1]; for (int k = 0, j = 0; j < temp.length; ++ k, ++ j){ if ( k == index ) { temp[index] = t; --k; } else { temp[j] = a[index]; // } } a = (T[]) temp; }
for (int i = last; i >= first; i--){ temp[i+1]=temp[i];}add()is usually that the list ends up larger, so the fact thatsizeremains unchanged is a huge warning sign!! --- Related to that, should the check beindex > sizeinstead ofindex >= a.length?