Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsreader4.netcologne.de!news.netcologne.de!nx02.iad01.newshosting.com!newshosting.com!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 09 Aug 2011 06:00:44 -0500 Date: Tue, 09 Aug 2011 04:00:44 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows NT 5.2; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: setSize ArrayList, when will it come? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 47 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 70.230.203.65 X-Trace: sv3-B15byrlPsUVk9crsl+//sg9SwzULrKRyZzUPsEUdh7SxhQWfElkQxti1ShtXFxYzXb4NS5HJPCxyiZP!k6JI7Upn+esIoDy8UkKRC4/G5RbLuMGmYjeAZU9Pt4SWQ8aSm+ICHaYK3bx2jtFHEVdTx+uTAk+q!EsKsrgD26zj4dk0lEBjFum5dp6InGp/59Fw5dVWghjMLVg== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2850 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6909 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 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