0

According to a book I was reading

There is an important distinction between the capacity of an array list and the size of an array. If you allocate an array with 100 entries, then the array has 100 slots, ready for use. An array list with a capacity of 100 elements has the potential of holding 100 elements (and, in fact, more than 100, at the cost of additional reallocations)— but at the beginning, even after its initial construction, an array list holds no elements at all.

However, we can also create an ArrayList without any capacity defined.

How will an ArrayList without any capacity defined and an ArrayList with capacity allocate its elements and when we should use one over the other?

The only thing striking my mind is, why would we have capacity if in both ways (ArrayList with capacity and ArrayList with no capacity), the values will be treated same way, so the only possible reason coming in my mind is that they both hold values differently.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
tycoon
  • 121
  • 9
  • "but how an ArrayList without any capacity defined" <- It can't. If you do not specify an initial capacity yourself it will just use a default value for its initial capacity. But that initial capacity is always defined and exists. – OH GOD SPIDERS Oct 05 '20 at 13:25
  • 4
    An `ArrayList` is (unsurprisingly) backed by an array. The main difference is that you don't need to care about *how* that array is managed. But it *is* managed. It *does* have an initial capacity and It *does* grow when needed. You can take a look at the source code for [`ArrayList`](https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/util/ArrayList.java). It's rather easy to follow. – Federico klez Culloca Oct 05 '20 at 13:25
  • 1
    Note that ArrayList is a specific **implementation** of the List interface. So: do not **assume** how that implementation might look like. Instead: look at the corresponding source code! – GhostCat Oct 05 '20 at 13:29
  • I think that, apart from having a default initial value for the capacity (this value is 10, btw), `ArrayList` defers allocation of the array until the first element is added. You should check `ArrayList` source code to get insight about these details – fps Oct 05 '20 at 13:31
  • @OHGODSPIDERS than what will be the advantage of defining a capacity during initialization? – tycoon Oct 05 '20 at 13:31
  • ok thanks i will look the source code – tycoon Oct 05 '20 at 13:32
  • 1
    @tycoon: See [Why start an ArrayList with an initial capacity?](https://stackoverflow.com/questions/15430247/why-start-an-arraylist-with-an-initial-capacity) – OH GOD SPIDERS Oct 05 '20 at 13:33

1 Answers1

0

The reason for providing an initial allocation is to pre-allocate the memory representation required to hold that many items, otherwise dynamic allocation is necessary for all item added. Note, dynamic allocation becomes necessary once the array contains the number of pre-allocated items.

MarkAddison
  • 515
  • 2
  • 9