Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java ArrayList

  • • ArrayList is an implementation class of List interface.
  • • Underlying data structure is resizable array.
  • • Duplicates are allowed.
  • • Insertion order is preserved.
  • • Null insertion is possible.
  • • Heterogeneous data items are allowed.
  • • Implements Serializable and Clonable and RandomAccess interface.

General hierarchy:
java.lang.Object
java.util.AbstractCollection
java.util.ArrayList
public class ArrayList
extends AbstractList
implements List, RandomAccess, Cloneable, Serializable

ArrayList Constructors

1. ArrayList() : Create an empty ArrayList object with default initial capacity 10. Once
ArrayList reaches its max capacity then new ArrayList will be created with a new
capacity.

new capacity=(current capacity*3/2)+1
i.e (10*3/2)+1=16
after this new ArrayList is created the previous one will be deleted and it will be treated as
garbage.

2. ArrayList(int initialcapacity) : Constructs an empty ArrayList with the specified
initial capacity given by the user.

3. ArrayList(Collection c) : For every Collection object, we can create an equivalent
ArrayList.

ArrayListExample1.java

importjava.util.*;
classArrayListExample1
{
	public static void main(String[] args)
	{
		ArrayList al=new ArrayList();
		al.add("Tapu");
		al.add("Satyajit");
		al.add(20);
		al.add(null);
		System.out.println(al); //[Tapu,Satyajit,20,null]
		al.remove(1);
		al.add(1,"Sasmita Samant");
		al.add("Nilan");
		System.out.println(al); //[Tapu,Sasmita,20,null,Nilan]
	}
}

Output
[Tapu,Satyajit,20,null]
[Tapu,Sasmita Samant,20,null,Nilan]

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext