The ArrayList – A fundamental data structure

Let’s take a look at a great data structure provided in Java – The ArrayList.

The ArrayList is a standard Java class which resides in the java.util package. In other words, you don’t have to add any libraries or dependencies to your project – the ArrayList is already there, or “pre-packaged” if you will.
The ArrayList is a dynamic array, which supports insertion and removal of objects. You can also search for objects in an ArrayList. But why should you use an ArrayList instead of the native array?

The native array in Java has very limited functionality.   Say, for example, that you want to expand your array. If you’re using a native array, there is no support for that. Of course, it is possible to implement a work-around, but it will be tedious and require extra work – and when you’re programming for a client, you don’t want that!
To further prove the need for the ArrayList, let’s say you want to remove a certain object from your list.
If you’re using a native array, you’ll have to manually loop through the entire array. And to complicate things even more, you’ll also have to shift the positions of every object previous to the object you removed. Something like this:

The above illustration does not include the logic to shift the positions. So, even more work on your part.. With an ArrayList, on the other hand:

myArrayList.remove(objectToRemove);

This is what makes the ArrayList so great. It has a whole lot of convenient methods, and will basically do all the heavy lifting for you.

Creating an ArrayList

Say you want to create an array, which stores a list of Strings. With a regular array, you would do this:

String [] stringArr = new String[10]

The number 10 signals that you want the array to be able to hold a total of 10 objects.
But we know better.. Let’s use an ArrayList:

ArrayList<String> stringArray = new ArrayList<String>();

Note that the last declaration, <String>, is redundant when using Java 7 or higher. Java 7 introduced the diamond operator, so the above statement is actually equivalent to:

ArrayList<String> stringArray = new ArrayList<>();

That’s about it. Notice how you don’t have to specify a size for your list. The ArrayList handles all this internally, and you never have to worry about “running out of space”.

Adding to an ArrayList

Let’s say you want to store a list of people invited to your birthday party. The ArrayList we created support String-objects, so this is perfectly fine. We’re assuming “John”, “Lian” and “Beth” are attending (hell of a party).
To add them to the list, we make use of the add() method of the ArrayList.

Removing objects from an ArrayList

There is actually two ways to remove objects from an ArrayList. If you know which position the object is at, you can pass this information to the remove() method. If you don’t know the position of the object, you can simply pass the object itself – the ArrayList will look for it, and remove the object if it’s in the array.
Remember that positions in an array always start at 0! Thus, the first object is at position 0.

So, to remove “John” from the list, which is in the first position (position 0), we do this:

stringArray.remove(0);

Otherwise, if we’d like to remove “John”, without knowing the objects position, we do it this way:

stringArray.remove("John");

Looping through an ArrayList

Now, as our last example, let’s say you want to have a look at all the people attending the party. You can do this in two different ways.
The “old school” method of doing this, is with a standard for loop. Let’s look at the code:

This is pretty straight forward. If you haven’t seen a for loop before, I encourage you to go and look it up!

The second way to loop (actually, iterate) through an ArrayList is with the enhanced for loop. The enhanced for loop makes use of an Iterator behind the scenes, and is even easier to implement than the for loop shown above.

What you’re basically saying is: For every String s in the stringArray, print it out! Pretty easy, eh?

Summary

In this article we talked about the ArrayList. The ArrayList is a dynamic array, and comes with a whole lot more functionality than a regular array does. The operations shown in this article is just a few out of many useful operations that the ArrayList supports.

If you’re curious about how the ArrayList works “under the hood”, I’ve written a simple implementation of an ArrayList. Most of the operations supported by the ArrayList in java.util are missing, but it’s great if you just want a feel for how it actually works. In fact, the ArrayList is just an abstracted regular array!
NB! If you want the enhanced for loop to work, you’ll have to implement an Iterator.

 

Tutorials will only get you so far..
If you wish to expand your Java skill-set and become an advanced Java developer, I personally recommend the Comprehensive Java Course by Edureka.
You can get 25% with the coupon code LIMITED25 😉

Leave a Reply

Your email address will not be published. Required fields are marked *