Why java.lang.IndexOutOfBoundsException is raised in this example, if the size of ArrayList has been predefined? How to solve this problem?
int size = 2:
ArrayList<Integer[]> nums = new ArrayList<Integer[]>(size);
Integer[] value1 = {1,2,3};
Integer[] value2 = {1,2};
nums.add(1,value1); // java.lang.IndexOutOfBoundsException
nums.add(0,value2);
You cannot put an item in an ArrayList before the other one is set. If you want to do it you’ll have to assign null values to the item on place 0 first.
It will work if you do:
edit/ Replaced add by set to replace the null object