Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Jan Burse Newsgroups: comp.lang.java.programmer Subject: Re: setSize ArrayList, when will it come? Date: Wed, 10 Aug 2011 20:22:39 +0200 Organization: albasani.net Lines: 35 Message-ID: References: <1eadnROad6gX8NzTnZ2dnUVZ_tydnZ2d@earthlink.com> <981db753-11a9-45da-aad4-a95c800cbab0@glegroupsg2000goo.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net Q/jHT8V8xsiIwEscatnDPJIu9ZaIBt+sCcU8F0Zx7dvx0SbRdO10k0ViDiX9HLoAd5iAYwvnQJTknECyQCQbmn+GFms1XSRo6AS8jWHIdWI2Vi1cBqBlY8nC4Wj1I7sL NNTP-Posting-Date: Wed, 10 Aug 2011 18:22:42 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="ImKr2WIONIZrHohnNlSnjR1SOfK9k/24RdqJ+puKN2BmJMFQI4QgJEZu+zEZbL/yXpV+QcJj/dkmh5fPqFG8I6zI4c1Gcxqhf5NGhUis5NKLFUabHrgO0IZSZsW2EvtJ"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110706 Firefox/5.0 SeaMonkey/2.2 In-Reply-To: <981db753-11a9-45da-aad4-a95c800cbab0@glegroupsg2000goo.googlegroups.com> Cancel-Lock: sha1:qNJKwN9YRMTfofn50WI/QSU6MbY= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6985 Lew schrieb: > 'ArrayList#ensureCapacity()'? > >> > So now you can go about and kill whatever you like. > Your objection? Although it is a public method, it wouldn't work, since it does not set the size field of the ArrayList. So you only have as a post condition after ensureCapacity(n): elementData.length>=n But not: size=n You would still need to invoke a couple of add() calls after calling ensureCapacity(). But calling ensureCapacity() before calling the add()s would prevent gradually enlarging the ArrayList, so that we would have a solution with zero unnecessary temporary objects. gr8 step forward, many thanks. But still not what I consider an efficient solution. You will burn some CPU cycles in the add()s call for nothing, since the elementData is already nulled from Array.newInstance() as required by the JLS. So not a green solution. Best Regards