Java Array Lists in 5 minutes

What is it?

In programming, there is a concept of static and dynamic arrays. We declare a static array in Java as a set size. I go into more details on static arrays in my Java array post. A dynamic array means we can initialize the array without a set size. The dynamic array will also grow as you add more items to the array. There are several different types of dynamic arrays in Java but this article will cover the ArrayList. I'll cover how to initialize an ArrayList, some helpful methods, and the pros and cons of using them.

sticky notes on a wall

Photo by Kelly Sikkema on Unsplash

How to Initialize

In Java, all dynamic arrays implement an interface called List. When using dynamic arrays it's considered a best practice to type the variable as List. This prevents getting coupled to a specific implementation of a list. If you're curious you can read the Oracle documentation on what implementations of List are available. List requires a type, whatever the type is the only type we can store in the List. We now know enough to declare the first half of our ArrayList.

List<String> nameOfList;

To initialize our array list we'll need to call the new keyword on the ArrayList class. When creating an ArrayList you can pass in a number to initialize the ArrayList with a size. This isn't needed since the ArrayList is dynamic and you'll almost never see an ArrayList instantiated with a size.

List<String> nameOfList = new ArrayList<>();

Helpful Methods

The ArrayList class has too many methods to cover. I would say knowing how to add, get, remove, and find the size is essential to using ArrayLists. Most other methods are situational and a quick Google search will be all you need. All the methods available on a Java ArrayList are here.

dogs wearing santas helper jackets

Photo by Karsten Winegeart on Unsplash

Pro's and Con's

The biggest benefit to using an ArrayList is the ever-increasing size. The other big benefit is the API methods provided: get, add, size, etc... However, this acts as a double-edged sword. ArrayLists will consume more memory and generally be more expensive to maintain compared to a simple array. Another potential downside to ArrayList's is that they only store objects. When trying to store an int in an ArrayList you first have to convert it to an Integer.

My reasoning is to always make sure you can't use an array before you create an ArrayList.

Advanced Knowledge

This is an advanced topic! I'm including it because it provides an interesting look at what goes on behind the scenes of an ArrayList. If you can follow along with this topic you have a solid grasp on ArrayLists.

What happens when our ArrayList runs out of size? We know from above that it will expand to fit more items but how?

When an ArrayList runs out of memory it will increase its size based on whatever the details under the hood specify. This could be doubling in size, tripling, or whatever, know that the ArrayList will expand.

The ArrayList will then copy the data of the old ArrayList over to the new ArrayList. This may sound very bad, a copy is an expensive operation, and if an ArrayList does this every time that must mean it's not very efficient right? On a small scale, yes. An add into an ArrayList could be O(n). Every single item in the ArrayList has to be iterated and copied over into the new, expanded ArrayList. However, as our ArrayList grows it will have to expand fewer times and this copy will occur less frequently. If we do this over and over we end up with big O(1), also called constant time.

This is the concept of amortized time. If we do something enough we don't care about the edge case of an expensive operation. That's why you'll see the performance of an add into an ArrayList shown as O(1) not O(n).