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.
Table of Contents
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.
List<String> myFavouriteCities = new ArrayList<String>(); ArrayList<int> scores = new ArrayList<int>();
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.
package com.itersdesktop.javatechs.collections; import java.util.ArrayList; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { List<String> myFavouriteCites = new ArrayList<String>(); myFavouriteCites.add("Quy Nhon"); myFavouriteCites.add("Ho Chi Minh City"); myFavouriteCites.add("Bordeaux"); myFavouriteCites.add("Paris"); // display the elements System.out.println(myFavouriteCites); // add "London" at the third position myFavouriteCites.add(2, "London"); // display the elements System.out.println(myFavouriteCites); } }
Complete and run the snippet above, the output likely looks
[Quy Nhon, Ho Chi Minh City, Bordeaux, Paris] [Quy Nhon, Ho Chi Minh City, London, Bordeaux, Paris]
[box type=”note” align=”aligncenter”]Since the index starts from 0, so the index of the third position is 2 instead 3.
[/box]
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:
package com.itersdesktop.javatechs.collections; import java.util.ArrayList; import java.util.List; public class ArrayListDemo { public static void main(String[] args) { List<String> myFavouriteCites = new ArrayList<String>(); myFavouriteCites.add("Quy Nhon"); myFavouriteCites.add("Ho Chi Minh City"); myFavouriteCites.add("Bordeaux"); myFavouriteCites.add("Paris"); myFavouriteCites.add("London"); myFavouriteCites.add("Cambridge"); // display the elements System.out.println(myFavouriteCites); // remove "Bordeaux" at the third position myFavouriteCites.remove(2); // display the elements System.out.println(myFavouriteCites); } }
The output of the snippet above likely looks
[Quy Nhon, Ho Chi Minh City, Bordeaux, Paris, London, Cambridge] [Quy Nhon, Ho Chi Minh City, Paris, London, Cambridge]
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:
for (String city : myFavouriteCites) { System.out.println(city); }
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.
- set(int index, Object o): used for updating an element.
- int indexOf(Object 0): returns the index of the specific object.
- Object get(int index): returns the object of the list