Java – ArrayList class with example

In this article, we will discuss ArrayList class – one of the List implemented class in detail

1. ArrayList:

  • ArrayList is implementation class of List interface (i.e.; ArrayList implements List)
  • ArrayList uses resizable array or grow-able array to store element/objects
  • Duplicate element/objects are allowed to be inserted
  • Insertion order is maintained
  • Allows NULL insertion (no limit, we can insert any number of NULL to ArrayList)
  • Without generics, ArrayList allows to insert any type of objects;
  • with generics, it is type-bounded (except, if we take Object as type within angle brackets)
  • Element retrieval is faster as ArrayList works on zero-based index
  • Manipulation (i.e.; addition/deletion of element from middle of ArrayList) is very slow, as it requires lot of shifting work internally
  • ArrayList is non-synchronized
  • Present in java.util package and extends java.util.AbstractList implements java.util.List interface
  • Also, implements java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable marker interfaces which provides special ability to ArrayList (provided by JVM at run time) like
  • java.util.Random Access: to access any random element/objects with same speed
  • java.lang.Cloneable: to create a duplicate object or to clone an object
  • java.io.Serializable: to transfer objects across network
4-ArrayList-in-java

Source: Team BenchResources.Net

2. ArrayList constructors:

2.1 ArrayList al = new ArrayList();

  • creates an empty ArrayList object of size 10
  • when maximum size of the ArrayList is reached, then new ArrayList of bigger size is created using below formula
    Capacity: New ArrayList size = (current size * 3/2) + 1;
  • old ArrayList item values will be copied into new ArrayList

2.2 ArrayList al = new ArrayList(int initialCapacity);

  • creates an empty ArrayList object of specified size (or initial capacity)
  • when maximum size is reached, above formula will be applied and new ArryaList will be created

2.3 ArrayList al = new ArrayList(Collection c);

  • created an equivalent ArrayList object for the specified collection
  • it is basically used for inter-conversion between collection objects

3. ArrayList examples:

ArrayListAddAndRemove.java

package in.bench.resources.java.collection;

import java.util.ArrayList;

public class ArrayListAddAndRemove {

	public static void main(String[] args) {

		// creating ArrayList object of type String
		ArrayList<String> al = new ArrayList<String>();

		// adding elements to ArrayList object
		al.add("Sundar Pichai");
		al.add("Satya Nadella");
		al.add("Shiv Nadar");
		al.add("Shantanu Narayen");
		al.add("Sundar Pichai");
		al.add("Francisco D’Souza");

		System.out.println("Iterating ArrayList values\n");

		// Iterating using enhanced for-loop
		for(String str : al){
			System.out.println(str);
		}

		// removing element at 4th index
		al.remove(4);

		// to print all values of ArrayList
		System.out.println("\n\nArrayList values after"
				+ " removal at 4th index postion \n\n" + al);
	}
}

Output:

Iterating ArrayList values

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Sundar Pichai
Francisco D’Souza

ArrayList values after removal at 4th index postion 

[Sundar Pichai, Satya Nadella, Shiv Nadar,
Shantanu Narayen, Francisco D’Souza]

Note: All methods of ArrayList is non-synchronized

Q) How to make ArrayList synchronized ?

  • ArrayList can be easily converted into synchronized ArrayList using utility method of java.util.Collections class
  • Syntax :
List list = Collections.synchronizedList(al);

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - LinkedList class with example
Java - List interface