This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am getting java.lang.IndexOutOfBoundsException: Index: 1, Size: 0 at java.util.ArrayList.add(ArrayList.java:360) I have tried running ensureCapacity(int) first, but the array list is always size 0. I need array like access,but want the convenience of the list interface. (i.e. i want to access items at a particular index) Is there a better solution?
You can do this, but first the size of the ArrayList has to be large enough. Note that size (the number of elements in the list) and capacity (the length of the array backing the list) are different. ensureCapacity() doesn't change the size. You could call list.add(null) a sufficient number of times to get you the empty slots that you need.