coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

ArrayList In Java

In this article, we will see ArrayList in Java. How to create an ArrayList? Its various types of constructors for creating an ArrayList object. Also, some important features with an example.

Introduction

ArrayList is a class in the Collections Framework. It is present in java.util package.

The ArrayList internally uses a dynamic array for storing the elements. So ArrayList is more flexible than the array.

The ArrayList class inherits the AbstractList class and it implements the List Interface.

Unlike Array, we need not fix the length of ArrayList at the time of initialization. As we add or remove elements from the ArrayList, it can grow and shrink dynamically.

The load factor for the ArrayList is 0.75F.

The default initial capacity is 10.

Load factor = 0.75 * 10 = 7

So when we try to add the 7th element to the ArrayList, it will create a new array double the size of the old array i.e. 20. Also, it will copy the old array in the new array and then add the element in a new array.

Constructors in the ArrayList

To create an ArrayList, we need to create an object of the ArrayList class. To create an object ArrayList provides three types of constructor

  1. ArrayList()

  2. ArrayList(Collection<? extends E> c)

  3. ArrayList(int initialCapacity)

Let’s see all one by one

1. ArrayList()

This is a simple constructor to create an ArrayList object with a default capacity of ten (10).

1
ArrayList<String> arrayList = new ArrayList<String>();

Above creating an empty array list object. The default initial capacity is 10.

2. ArrayList(Collection<? extends E> c)

This constructor constructs a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.

1
2
3
4
5
6
ArrayList<String> anotherCollection = new ArrayList<String>();
anotherCollection.add("example");

// 2nd constructor example is the below line of code
ArrayList<String> arrayList = new ArrayList<String>(anotherCollection);

3. ArrayList(int initialCapacity)

This constructor constructs an empty list with the specified initial capacity.

1
ArrayList<String> arrayList = new ArrayList<String>(20);

Above, I am creating an empty array list object with it’s initial capacity as 20.

Note:

1
ArrayList<String> arrayList = new ArrayList<String>(20);

In the above code the <String> represents Generics and I am creating an arrayList object which will be storing String type of objects only. It will give us a compile-time error if we try to add any other type of object to the arrayList.

By default, Collections are heterogeneous i.e. they can save all types of objects in one collection. By using Generics I am making it homogeneous i.e. it will store only specified types of objects in that particular collection.

Methods in ArrayList

Method Description
add(E e) This method adds an element to the end of the list.
add(int index, E element) This method adds an element to the list at the specifed index.
addAll(Collection c) This method adds the elements of the specified collection to the end of the ArrayList.
addAll(int index, Collection c) This method adds the elements of the specified collection at the specified index index.
get(int index) This method is used to get the ArrayList element by it’s index index.
set(int index, E element) This method sets the sepecified element at the specifed index.
remove(Object o) This method removes the specified object from the ArrayList.
remove(int index) This method removes the object at specified index from the ArrayList.
removeIf(Predicate filter) This method removes all of the elements of the ArrayList that satisfy the given predicate.
removeAll(Collection c) This method removes from this list all of its elements that are contained in the specified collection.
iterator() This method provides the Iterator to iterate over an ArrayList.
clear() This method removes all the elements from the list and makes it empty.
isEmpty() This method returns true if this list contains no elements.
contains() This method is used for checking if the specified element exists in the given list or not.
toArray() This method returns an array containing all of the elements in this ArrayList in proper sequence.
toArray(T[] a) This method returns T [] i.e. An array of the specified type T can be returned.
removeRange(int fromIndex, int toIndex) This method is used for removing the range of elements from the subclasses of the ArrayList class. The removeRange(int fromIndex, int toIndex) method is a protected method.
retainAll(Collection c) This method retains all the elements of the collection passed as a parameter in the list.
trimToSize() This method trims the capacity of this ArrayList instance to be the list’s current size.
indexOf(Object o) This method returns the index of the first occurrences of the object specified. It returns -1 if the specified object o is not present in the list.
lastIndexOf(Object o) This method returns the index of the last occurrences of the object specified. It returns -1 if the specified object o is not present in the list.
ensureCapacity() This method increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
listIterator() This method returns a fail-fast list iterator over the elements in this list (in proper sequence).

Features

1. ArrayList maintains the insertion order.

I have given a simple Java program below that shows, ArrayList maintains the insertion order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.ArrayList;

/**
 * A Java program showing an ArrayList maintains the insertion order.
 * 
 * @author coderoll.com
 *
 */
public class ArrayListExample {

	public static void main(String[] args) {
		
		ArrayList<String> arrayList = new ArrayList<String>();
		
		arrayList.add("Gaurav");
		arrayList.add("Shyam");
		arrayList.add("Saurav");
		arrayList.add("Samiksha");
		arrayList.add("Rina");
		
		System.out.println(arrayList);
	}
}

Output

1
[Gaurav, Shyam, Saurav, Samiksha, Rina]

Explanation

  1. In the above program, I have created an arrayList object with a type one constructor. So the default initial capacity of arrayList is 10.
  2. I have added 5 string objects using the add() method one by one.
  3. I have printed the arrayList object using a sysout statement. Here we can observe that all the strings are in the same order that they are inserted in the code. So we can say that the ArrayList maintains the insertion order.

2. ArrayList allows the duplicate elements.

I have given a simple Java program below that shows, that ArrayList allows duplicate elements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.ArrayList;

public class ArrayListDuplicate {

  public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();
    
    arrayList.add("Gaurav");
    arrayList.add("Shyam");
    arrayList.add("Saurav");
    arrayList.add("Samiksha");
    arrayList.add("Rina");
    arrayList.add("Rina"); //adding a duplicate string object 
    
    System.out.println(arrayList);
  
  }
}

Output

1
[Gaurav, Shyam, Saurav, Samiksha, Rina, Rina]

Explanation

  1. In the above program, I have created an arrayList object with a type one constructor. So the default initial capacity of arrayList is 10.
  2. I have added 6 string objects using the add() method one by one. You can observe the 5th and 6th String objects are the same. i.e duplicate ("Rina" )
  3. On printing the arrayList, we can see the 5th as well as 6th String object ("Rina" ) on the console. That means ArrayList will allow the duplicate elements.

Common Operations on ArrayList

1. Adding an element in the ArrayList

When we create an ArrayList Object, we can add an element to the ArrayList using the add(Object o) method and add(int index, Object o) method.

Read more and check example Java programs about adding an element to the ArrayList in this blog: How To Add An Element To ArrayList In Java?

2. Changing an element of the ArrayList

When you create an ArrayList object and add elements to it, sometimes you need to change the element at a particular index. You can do it using the set(int index, E element).

Read more and check example Java programs about changing the element of ArrayList in this blogpost: How To Change An Element In ArrayList?

3. Removing an element of the ArrayList

If you want to remove the element from the ArrayList, you can use the remove(Object o) method or remove(int index) method.

Read more and check an example Java program about removing an element from the ArrayList on this blogpost: How To Remove An Element From An ArrayList?

4. Iterating over an ArrayList

There are multiple ways to iterate over an ArrayList. You can use the traditional for loop or for-each loop or Iterator.

I have covered all the ways of iterating the ArrayList in Java with all the examples at Iterating the ArrayList In Java


The example Java programs given in the above tutorial can be found at this GitHub Repository.

Let me know your thoughts or suggestions in the comment section below.

Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus