Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

9
  • 1
    Your preallocation argument is invalid, because this is already build into ArrayList with the ensureCapacity(int minCapacity) method and the ArrayList(int initialCapacity) constructor. Commented Sep 15, 2014 at 15:10
  • 7
    @Philipp What values will it put into that space? Commented Sep 15, 2014 at 17:25
  • The obvious choice is to put a null in the pre-allocated (but as yet unused) entries of the ArrayList; but we can let ensureCapacity do that and yet not allow other functions such as set to do it. The reasons given elsewhere seem stronger. Commented Oct 9, 2014 at 12:45
  • @DavidK: It makes sense to say that it should be possible to set an item to any value which could be returned by get. Even if a normal attempt to get an item for which space had been allocated but never written should throw an exception rather than returning null, it would still be useful to have a pair of methods that would allow list1.setOrEraseIfNull(index, list2.getOrReturnNull(index)) rather than requiring if (list2.valueSet(index)) list1.set(index, list2.get(index)); else list1.unset(index); Commented Oct 13, 2014 at 19:07
  • 1
    @DavidK: Attempting to read an entry which has been allocated but not yet written must either yield null or throw an exception; it's easier to have it return null. Commented Oct 14, 2014 at 1:58