Either I’m doing this wrong or i’m not understanding how this method works.
ArrayList<String> a = new ArrayList<String>();
a.ensureCapacity(200);
a.add(190,"test");
System.out.println(a.get(190).toString());
I would have thought that ensureCapacity would let me insert a record with an index up to that value. Is there a different way to do this?
I get an IndexOutOfBounds error on the third line.
No,
ensureCapacitydoesn’t change the logical size of anArrayList– it changes the capacity, which is the size the list can reach before it next needs to copy values.You need to be very aware of the difference between a logical size (i.e. all the values in the range
[0, size)are accessible, and adding a new element will add it at indexsize) and the capacity which is more of an implementation detail really – it’s the size of the backing array used for storage.Calling
ensureCapacityshould only ever make any difference in terms of performance (by avoiding excessive copying) – it doesn’t affect the logical model of what’s in the list, if you see what I mean.EDIT: It sounds like you want a sort of
ensureSize()method, which might look something like this: