Java's ArrayLists are like smart arrays. You can't change an array's length, but with an ArrayList, you don't have to worry about the length because the ArrayList's class magically handles it for you.
How do ArrayLists work?
Under the hood, ArrayLists are arrays but are auto-expanding. The ArrayList class keeps track of how many items are in the array, and when you want to add another item, it expands as necessary. The length of an array can't be changed, so to expand the array, the ArrayList class creates a new array with the required capacity and copies in the values. This process can be time-consuming and use system resources, especially for large ArrayLists. By default, ArrayLists start off with a capacity of 10, but you can specify an initial capacity using the constructor.
Note I didn't say auto-sizing. ArrayLists do not decrease their size. If you add 1,000,000 items to an ArrayList and then decide you want to remove them all except three, your ArrayList is still using 1,000,000 items worth of memory. You can reduce the used memory using the trimToSize() method, which creates an array big enough to hold the current size (reducing capacity), but this isn't called automatically, and you could encounter memory issues if you create an extremely large ArrayList and never resize it.
Why won't it scale down? Recreating the array is resource-intensive, so Java doesn't do it automatically. Since resizing is so demanding, it's a best practice to set the initial capacity as high as you anticipate needing in the constructor to minimize the amount of times it has to scale up.
If you want to dive deeper, try viewing the implementation within your IDE.
Size vs Capacity
Now that we've discussed how ArrayLists work at a high level let's look at the differences between size and capacity:
Size
- Accessible using the size() method.
- The number of used values in the array (not the capacity).
- The number of elements currently held (not the number of potential elements).
Capacity
- Not accessible.
- Greater than or equal to the size.
- The length of the internal array used by the ArrayList class.
Interestingly enough, when you inspect the implementation of Java's array list, the size is just an integer. Furthermore, the capacity is found internally by getting the length of the elementData (the name of the underlying Array). The size is used to keep track of how many elements are in the elementData, while the capacity is only used for determining if a resizing operation needs to take place.
As you can see, size and capacity mean two different things in the context of Java ArrayLists. Even though we as programmers can't access an ArrayList's capacity, it's still important to remember it's there and that we should trim the capacity as needed.
Top comments (0)