java.util.ArrayList Class

The ArrayList class is the Collection Framework's replacement for the Vector class. Functionally equivalent, their primary difference is that ArrayList usage is not synchronized by default, whereas Vector is. Both maintain their collection of data in an ordered fashion within an array as their backing store.

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.

The array provides quick, random access to elements at a cost of slower insertion and deletion of those elements not at the end of the list. If you need to frequently add and delete elements from the middle of the list, consider using a LinkedList.

ArrayList shares some similarities with HashSet and TreeSet and provides some behavior that is not the same. The base implementation class is similar to HashSet and TreeSet both extend from the AbstractCollection superclass. However, instead of further extending from AbstractSet, ArrayList extends from AbstractList. Unlike the sets, ArrayList supports storing duplicate elements. While much of the ArrayList behavior is inherited from AbstractList, the class still needs to customize the majority of its behavior.

ArrayList class has following constructors.

ArrayList()

Constructs an empty list with an initial capacity of ten.

ArrayList(Collection<? extends E> c)

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

ArrayList(int initialCapacity)

Constructs an empty list with the specified initial capacity.

In this tutorial you can learn about java.util.ArrayList class and its examples. And also learn how to use java.util.ArrayList class.

java.util.ArrayList class Example
/* Save with file name ArrayListExample.java */

import java.util.ArrayList;

public class ArrayListExample
{
	public static void main(String args[])
	{
		//java.util.ArrayList DECLARATION
		ArrayList al;

		//java.util.ArrayList OBJECT CREATION
		//USING DEFAULT CONSTRUCTOR
		al = new ArrayList();

		//ADD AN ELEMENT
		al.add("Huda Tutorials");
		al.add("Java Tutorials");

		//DUPLICATES ARE ALLOWED
		al.add("Huda Tutorials");
		al.add("C Tutorials");
		al.add("CPP Tutorials");

		//null ALLOWED
		al.add(null);

		//RETURNS COUNT OF ELEMENTS ArrayList CONTAINS
		System.out.println("Elements Count : " + al.size());

		//java.util.ArrayList OUTPUT
		System.out.println(al);
	}
}
                        

The following example shows how to use ArrayList with Iterator.

java.util.ArrayList class Example 2
/* Save with file name ArrayListExample2.java */

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListExample2
{
	public static void main(String args[])
	{
		//java.util.ArrayList DECLARATION
		ArrayList al;

		//java.util.ArrayList OBJECT CREATION
		//USING DEFAULT CONSTRUCTOR
		al = new ArrayList();

		//ADD AN ELEMENT
		al.add("Huda Tutorials");
		al.add("Java Tutorials");

		//DUPLICATES ARE ALLOWED
		al.add("Huda Tutorials");
		al.add("C Tutorials");
		al.add("CPP Tutorials");

		//null ALLOWED
		al.add(null);

		//RETURNS COUNT OF ELEMENTS ArrayList CONTAINS
		System.out.println("Elements Count : " + al.size());

		//java.util.ArrayList OUTPUT
		Iterator itr = al.iterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}
	}
}
                        

The following example shows how to use ArrayList with Arrays, List and Iterator.

java.util.ArrayList class Example 3
/* Save with file name ArrayListExample3.java */

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Iterator;

public class ArrayListExample3
{
	public static void main(String args[])
	{
		String elements[] = {"Huda Tutorials","Java Tutorials","C Tutorials"};
		List s; //java.util.List DECLARATION

		//java.util.List OBJECT CREATION USING ARRAY AS LIST
		s = new ArrayList(Arrays.asList(elements));

		//java.util.List OUTPUT
		Iterator itr = s.iterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}
	}
}
                        
java.util.ArrayList class Example 4
/* Save with file name ArrayListExample4.java */

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Iterator;

public class ArrayListExample4
{
	public static void main(String args[])
	{
		String elements[] = {"Huda Tutorials","Java Tutorials","C Tutorials"};
		List s; //java.util.List DECLARATION

		//java.util.List OBJECT CREATION USING ARRAY AS LIST
		s = new ArrayList(Arrays.asList(elements));

		//ADD ELEMENT TO List COLLECTION
		s.add("NetBeans Tutorials");

		//DISPLAY SIZE OF THE List COLLECTION
		System.out.println("List Collection Size : "+s.size());

		//CHECK THE List COLLECTION IS EMPTY
		System.out.println("List Collection is Empty : "+s.isEmpty());

		//CHECK THE GIVEN ELEMENT IN List COLLECTION
		System.out.println("\"Huda Tutorials\" Contains :"+s.contains("Huda Tutorials"));

		//java.util.List OUTPUT
		System.out.println();
		System.out.println("Elements Before Element Remove");
		System.out.println("------------------------------");
		Iterator itr = s.iterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}

		s.remove("C Tutorials");

		System.out.println();
		System.out.println("Elements After Element Remove");
		System.out.println("------------------------------");
		itr = s.iterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}
	}
}
                        

The following example shows how to compare two List Collections.

java.util.ArrayList class Example 5
/* Save with file name ArrayListExample5.java */

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Iterator;

public class ArrayListExample5
{
	public static void main(String args[])
	{
		String elements[] = {"Huda Tutorials","Java Tutorials","C Tutorials"};
		String elements2[] = {"Huda Tutorials","Java Tutorials","Android Tutorials"};

		//java.util.List DECLARATION
		List s, s2;

		//java.util.List OBJECT CREATION USING ARRAY AS LIST
		s = new ArrayList(Arrays.asList(elements));
		s2 = new ArrayList(Arrays.asList(elements2));

		//COMPARE TWO COLLECTIONS
		System.out.println("Equals : " + s2.equals(s));

		//CONTAINS COLLECTION IN OTHER COLLECTION
		System.out.println("Contains : " + s2.containsAll(s));
	}
}
                        

The following example shows how to save List Collection into file.

java.util.ArrayList class Example 6
/* Save with file name ArrayListExample6.java */

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class ArrayListExample6
{
	public static void main(String args[])
	{
		try
		{
			String elements[] = {"Huda Tutorials","Java Tutorials","Android Tutorials"};

			//java.util.ArrayList DECLARATION
			List s;

			//java.util.List OBJECT CREATION USING ARRAY AS LIST
			s = new ArrayList(Arrays.asList(elements));

			//FileOutputStream CREATION
			FileOutputStream fos = new FileOutputStream("List.set");

			//ObjectOutputStream CREATION
			ObjectOutputStream oos = new ObjectOutputStream(fos);

			//WRITE List OBJECT TO ObjectOutputStream
			oos.writeObject(s);

			//CLOSE THE ObjectOutputStream
			oos.close();
			System.out.println("List Collection Saved into File Sucessfully");
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : " + e.getMessage());
		}
	}
}
                        

The following example shows how to retrieve List Collection from file.

java.util.ArrayList class Example 7
/* Save with file name ArrayListExample7.java */

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class ArrayListExample7
{
	public static void main(String args[])
	{
		try
		{
			//java.util.ArrayList DECLARATION
			List s;

			//FileInputStream CREATION
			FileInputStream fis = new FileInputStream("List.set");

			//ObjectInputStream CREATION
			ObjectInputStream ois = new ObjectInputStream(fis);

			//READ List OBJECT FROM ObjectInputStream
			s = (List) ois.readObject();
			ois.close();
			System.out.println(s);
		}
		catch(Exception e)
		{
			System.out.println("Error Occurred : " + e.getMessage());
		}
	}
}
                        

The following example shows how to use ArrayList with Arrays, List and ListIterator.

java.util.ArrayList class Example 8
/* java.util.ArrayList class Example 8 */
/* Save with file name ArrayListExample8.java */

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.ListIterator;

public class ArrayListExample8
{
	public static void main(String args[])
	{
		String elements[] = {"Huda Tutorials","Java Tutorials","C Tutorials"};
		List s; //java.util.List DECLARATION

		//java.util.List OBJECT CREATION USING ARRAY AS LIST
		s = new ArrayList(Arrays.asList(elements));

		//java.util.List OUTPUT
		ListIterator itr = s.listIterator();
		while(itr.hasNext())
		{
			System.out.println(itr.next());
		}

		System.out.println();
		System.out.println("----- Reverse Order -----");
		System.out.println();

		//java.util.List OUTPUT IN REVERSE ORDER
		ListIterator itr2 = s.listIterator(s.size());
		while(itr2.hasPrevious())
		{
			System.out.println(itr2.previous());
		}
	}
}