After creating a
ArrayList<Integer> connectedTo = new ArrayList<>();
connectedTo.add(0,4);
connectedTo.add(3,4);
In the above mentioned statement connectedTo.add(0,4) inserts a value 4 in the zeroth position which is as expected.
But connectedTo.add(3,4) throws a java.lang.IndexOutOfBoundsException. why is this happening?. From my understanding ArrayList are suppose to resize dynamically and allow insertions right?.
Is there any way to overcome this?. Incase of python creating a expression
vertList = {}
vertList[0] = 1
vertList[2] = 3
allows me to save the values in various indexes without any issues. How to implement a code perform's the same operation.