Re: setSize ArrayList, when will it come?
On 8/9/2011 2:16 AM, Jan Burse wrote:
Patricia Shanahan schrieb:
On 8/8/2011 7:16 PM, Jan Burse wrote:
...
I actually do use setSize for a kind of sparse Vector.
Sparse in the sense that my Vector will have a couple
of holes represented by null value elements. Which
is eventually abuse of the term "sparse", but the use
case is there.
...
If you only need small numbers of null elements, you could write a class
extending ArrayList that has setSize(). All you would do is loop adding
null elements or removing the tail elements until the ArrayList is the
required size.
Patricia
If only so many fields in ArrayList would not be private
I could do that. But since for example in JDK 1.6.0_26
none of the fields are protected, everything is private.
What you suggest is theoretically sound but practically
impossible. Look see:
public class ArrayList<E> extends ...
{
private transient Object[] elementData;
private int size;
...
}
And using reflection overriding this protection,
is kind of ugly and eventually less performant.
I meant "loop adding null elements" literally.
while(size() < targetSize) {
add(null);
}
That uses only the public methods size and add. It would not be
efficient for a significantly sparse array, but should work fine if
there are only a few null elements. If there are large blocks of null
elements and efficiency matters, I would use neither Vector nor ArrayList.
Patricia