ArrayList class

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

 

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)

Random Access: to access any random element/objects with same speed

Cloneable: to create a duplicate object or to clone an object

Serializable: to transfer objects across network

4-ArrayList-in-java

Source: Team BenchResources.Net

 

ArrayList constructors:

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

 

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

 

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

 

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 but it can be easily converted into synchronized ArrayList using utility method of java.util.Collections class

List list = Collections.synchronizedList(al);

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
https://docs.oracle.com/javase/tutorial/collections/implementations/list.html
https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

 

Happy Coding !!
Happy Learning !!

LinkedList class
List interface