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: Tue, 09 Aug 2011 11:16:48 +0200 Organization: albasani.net Lines: 35 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net WUB9nroUNQjMD0hS6AJUNlHxZh8VNH72nkZYN0GnuAVuFqnmqYUDY4xYB6JpC3NdzuS8kv549XWNnSjvyKGTkA== NNTP-Posting-Date: Tue, 9 Aug 2011 09:16:52 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="SMbus6yoXV2t9TeIHzzXmmwe0z0RBQchrOWepeDIe8bUHwQLUMu76ju+9GIoHgMZZu4lIclDbQfvgF9MqWgjRspG3z8ysY3N+/9PjNZpjtNwVqPhQRexxYwHBzUjiwaq"; 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: Cancel-Lock: sha1:yi5XLW5MvMHQGR7aOWVPtLhxKT8= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6903 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. Bye