ArrayList in Java with Example | Java ArrayList Hierarchy, Constructors, Methods, Creating a ArrayList

Well organized and easy to understand Java ArrayList tutorial with lots of examples is here for all java passionate. Beginners and experienced candidates can easily grasp the concept of ArrayList in Java. Also, You can use these provided sample Java ArrayList Examples to add & remove elements available in the list.

This ArrayList in Java Tutorial Helps To Learn:

Java ArrayList Class

The ArrayList in Java is a class that is present in java.util package. The ArrayList is a part of Collection Framework and it extends AbstractList and implements the List interface. Most of the developers use ArrayList over Array because it overcomes the limitation of Arrays.

ArrayList in Java with Example 1

Do Check: 

Java ArrayList Vs Array | Why ArrayList is better than Array?

In Java, the Arrays are of a fixed length, we cannot grow or shrink the Arrays size once we declared it, which means that you must know in advance how many elements in an array will hold. But sometimes you may not know how large an array you need. To handle this situation, the collection framework defines ArrayList.

ArrayList can dynamically increase or decrease in size. ArrayList is created with an initial size when this size is increased, the collection is automatically enlarged and when objects are removed the array may be shrunk.

Arrays contain the only homogenous type of elements whereas we can store both homogenous as well as heterogeneous elements in ArrayList. Apart from these benefits, ArrayList contains prefined methods that make the task easier for developers.

How ArrayList Work Internally?

From the below points, you can easily get a basic idea on working of the array when it fulls or when we try to add an item into the list:

  • Creates a bigger-sized memory on heap memory (for instance memory of double size).
  • Copies the current memory elements to the new memory.
  • A new item is added now as there is bigger memory available now.
  • Delete the old memory.

Constructors in the ArrayList Class

ArrayList has three constructors which are shown below:

1. ArrayList(): The first constructor is used to builds an empty ArrayList.

2. ArrayList(Collection c): The second constructor is used to builds an array list that is initialized with the elements of the collection c.

3. ArrayList(int capacity): The third constructor is used to builds an array list that has the specified initial capacity. The capacity is the size of the array that is used to store the elements. The capacity grows dynamically as elements are added to an array list.

Methods of ArrayList Class

ArrayList contains prefined methods that make the task easier for developers. The few important methods of the ArrayList class is discussed below:

1. add(Object obj): This method is used to add an object obj to the ArrayList.

2. add(int index, Element e): This method is used to insert an element at the specified position in an ArrayList.

3. remove(Object obj): This method is used to remove an element from ArrayList.

4. remove(int index, Element e): This method is used to remove an element from a given index.

5. protected void removeRange(int fromIndex, int toIndex): This method is used to remove the elements from the list which lies in the given range.

5. int size(): This method is used to find the size of the ArrayList.

6. void clear(): This method is used to remove all of the elements from the ArrayList.

7. boolean isEmpty(): This method is used to check the list is empty or not if the list is empty it returns true otherwise it returns false.

8. boolean contains(Object obj): This method is used to check whether the given object obj is present in the array list if it’s there then it returns true otherwise it returns false.

9. int indexOf(Object obj): This method is used to find out the index position of the element, if the list does not contain this element then this method returns the value -1.

10. set(int index, Element e): This method is used to replace the element at the specified position in this list with the specified element.

11. Object get(int index): This method is used to returns the element at the specified position in the list.

Creating an ArrayList

We can create an ArrayList by writing a simple statement which is shown below:

ArrayList al = new ArrayList();

This statement creates an array list with the name al which is of type “String”. The type determines which type of elements the ArrayList should contain. Since it is of type String, the elements that are going to be added in this list will be of String type.

Basic Operations on ArrayList

The ArrayList Class implements multiple methods to do different operations on array lists. Take a look at the most commonly used ArrayList operations in this tutorial:

  • Add elements
  • Access elements
  • Change elements
  • Remove elements

Performing Various Operations on ArrayList

1. Adding Elements: How to add elements to an ArrayList?

We can add elements to an array list by using two methods add() or add(int index, Element e). If you want to add an element at the end of the list then you can use add() method. If you want to add an element at the specified position in the list then you can use add(int index, Element e).

Let’s understand this with a simple example:

import java.util.*;
class Example{
public static void main(String args[]){

//creating an array list
ArrayList al = new ArrayList();

//adding elements to the list
al.add("Amit");
al.add("Rahul");
al.add("Raam");
al.add("Shyam");
al.add("Tom");
al.add("Karan");

//displaying elements
System.out.pritnln("After adding the above elements the list will be :" +al);

al.add(2, "Prashant");

//displaying elements after adding at a given index.
System.out.println("After adding the element at a given index the list will be: " + al);
}
}

Output:

After adding the above elements the list will be:
[Amit, Rahul, Raam, Shyam, Tom, Karan]
After adding the element at a given index the list will be:
[Amit, Rahul, Prashant, Raam, Shyam, Tom, Karan]

2. Removing Elements: Java ArrayList example to remove elements

We can use the remove() method to remove the element from an ArrayList. Let’s understand this with an example.

import java.util.*;
class Example{
public static void main(String args[]){

//creating an array list
ArrayList al = new ArrayList();

//adding elements to the list
al.add("Amit");
al.add("Rahul");
al.add("Raam");
al.add("Shyam");
al.add("Tom");
al.add("Karan");

//remove Rahul and Raam
System.out.println("After removing Raam and Rahul the list will be:" +al);

//remove the 2nd element
al.remove(1);

//displaying elements after removing above element
System.out.println("After removing the 2nd element the list will be: " + al);
}
}

Output:

After removing the Raam and Rahul the list will be :
[Amit, Shyam, Tom, Karan]
After removing the 2nd element the list will be:
[Amit, Tom, Karan]

Removing All Elements in the List – Java ArrayList Example

We can use the clear() method to remove all the elements from an ArrayList. Let’s understand this with an example

import java.util.*;
class Example{
public static void main(String args[]){
//creating an array list
ArrayList al = new ArrayList();

//adding elements to the list
al.add("Amit");
al.add("Rahul");
al.add("Raam");
al.add("Shyam");
al.add("Tom");
al.add("Karan");

//displaying initial elements
System.out.pritnln("After adding above elements the initial list will be:" +al);

al.clear();

//displaying elements after remove all the elements from a list.
System.out.println("After removing all elements the list will be: " + al);
}
}

Output:

After adding the above elements the initial list will be:
[Amit, Rahul, Raam, Shyam, Tom, Karan]
After removing all the elements the list will be: [ ]

ArrayList in Java Example on Checking the List is empty or not

We can use the isEmpty() method to check list is empty or not. Let’s understand this with an example.

import java.util.*;
class Example{
public static void main(String [] args){

ArrayList al=new ArrayList();
System.out.println("Is ArrayList Empty: "+al.isEmpty());

al.add("Amit");
al.add("Rahul");
al.add("Raam");

System.out.println("After Insertion");
System.out.println("Is ArrayList Empty: "+al.isEmpty());

}
}

Output:

Is ArrayList Empty: true
After Insertion
Is ArrayList Empty: False

Example of Java ArrayList to find the Length of ArrayList

We can use the size() method to find the length of an ArrayList. Let’s understand this with an example.

import java.util.*;
class Example{
public static void main(String args[]){

//creating an array list
ArrayList al = new ArrayList();

System.out.println("Initially, the size of list will be: " +al.size());

//adding elements to the list
al.add("Amit");
al.add("Rahul");
al.add("Raam");
al.add("Shyam");
al.add("Tom");
al.add("Karan");

//displaying size of an array list
System.out.pritnln("After adding above elements the size of the list will be :" +al.size());
}
}

Output:

Initially, the size of the list will be: 0
After adding the above elements the size of the list will be: 6