Vote count:
0
If have following class:
class CapacityTrackingArrayList<T> extends ArrayList<T>
{
public boolean add(T elem)
{
System.out.printf("Invoking add()... \n");
boolean isAdded = super.add(elem);
return isAdded;
}
public void ensureCapacity(int arg)
{
System.out.printf("Invoking ensureCapacity(%d)... \n", arg);
super.ensureCapacity(arg);
}
} // class CapacityTrackingArrayList
and testing with...
CapacityTrackingArrayList<Integer> numberList2 = new CapacityTrackingArrayList<Integer>();
numberList2.ensureCapacity(100);
for (int i = 0; i <= 5; i++)
{
numberList2.add(i);
}
I get following output
Invoking ensureCapacity(100)...
Invoking ensureCapacity(1)...
Invoking add()...
Invoking ensureCapacity(2)...
Invoking add()...
Invoking ensureCapacity(3)...
Invoking add()...
Invoking ensureCapacity(4)...
Invoking add()...
Invoking ensureCapacity(5)...
What strikes me is that, even though I call
numberList2.ensureCapacity(100);
before adding, I would expect the list to keep this capacity until about 100 elements have been added.
Apparently, not at all!
Always, when adding an element, it invokes ensureCapacity() ?? How come?
regards Chris
asked 45 secs ago
Aucun commentaire:
Enregistrer un commentaire