Home / Computer Science / Data Structures / ArrayList in Java

ArrayList in Java

Print Friendly, PDF & Email

ArrayList class implements List interface and it is based on Array data structure. It is widely used because of the functionality and flexibility that it offers. Most of the developers choose ArrayList instead of Array as it is considered as a good alternative of the traditional Java arrays. The below image shows the class diagram of List interface and its implementation.

Why ArrayList is better than Array?

The limitation of a traditional array is that is has a fixed length so that we cannot add any more elements when it is full. In terms of memory consumption, an array will eat the same amount of memory when it has a number of elements getting removed from or added to it.

On the other hand, an ArrayList can dynamically grow and shrink after addition and subtraction elements.

How to create an ArrayList?

Like most other data structures, we use the following statement to create/declare a variable/an object/an instance of the ArrayList class.

How to add elements to an ArrayList?

We can add elements to an ArrayList by using add() method which has a couple of variations. For example, we add an element at the end of the given list by default if doing the statement below.

myFavouriteCites.add("Cambridge"); // this will add "Cambridge" at the end of the list

But if you want to add the element at an arbitrary position in the list, we can specify the index in add() method like this:

myFavouriteCites.add(2, "Cambridge"); // this will add "Cambridge" at the second position in the list

Let’s complete a runnable snippet code.

Complete and run the snippet above, the output likely looks

Since the index starts from 0, so the index of the third position is 2 instead 3.

How to remove elements from ArrayList?

To remove elements from an ArrayList, we use remove() method which also has few variations like add() method. In other words, it facilitates to remove an element at an arbitrary position in the list. For example:

The output of the snippet above likely looks

How to iterate on an ArrayList?

The way we have displayed the content of ArrayList in the last two examples is simple and not the right manner to go through a list. The traditional, common and correct syntax to iterate on a list in Java should be similar to the following line:

Other methods of the ArrayList class

In the above examples, we have demonstrated two basic methods such as add() and remove(). However, there are a number of methods which can be used directly from an ArrayList instance. Let’s discuss a few important methods below.

  1. set(int index, Object o): used for updating an element.
  2. int indexOf(Object 0): returns the index of the specific object.
  3. Object get(int index): returns the object of the list

About Nguyen Vu Ngoc Tung

I love making new professional acquaintances. Don't hesitate to contact me via nguyenvungoctung@gmail.com if you want to talk about information technology, education, and research on complex networks analysis (i.e., metabolic networks analysis), data analysis, and applications of graph theory. Specialties: researching and proposing innovative business approaches to organizations, evaluating and consulting about usability engineering, training and employee development, web technologies, software architecture.