I have the below piece of code for swapping.
public static <E> void swap(List<E> list, int i, int j){
E temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
Now when I use List backed by Integer array like below
Integer[] ar = new Integer[]{1,2};
swap(Arrays.asList(ar),1,0);
It works fine and gives output as [2,1]
But I use List backed by int array like below
int[] ar = new int[]{1,2};
swap(Arrays.asList(ar),1,0);
It hrows ArrayIndexOutOfBounds exception. I don't understand why this is happening. List should treat int element as object only. Little help please.