Using the ArrayList in Java

Using the ArrayList in Java

Standard arrays in Java are fixed in the number of elements they can have. If you want to increase of decrease the elements in an array then you have to make a new array with the correct number of elements from the contents of the original array. An alternative is to use theArrayListclass. TheArrayListclass provides the means to make dynamic arrays (i.e., their length can increase and decrease).

Import Statement

import java.util.ArrayList;

Create an ArrayList

AnArrayListcan be created using the simple constructor:

ArrayList dynamicArray = new ArrayList();

This will create anArrayListwith an initial capacity for ten elements. If a larger (or smaller)ArrayListis required the initial capacity can be passed to the constructor. To make space for twenty elements:

ArrayList dynamicArray = new ArrayList(20);

Populating the ArrayList

Use the add method to append a value to theArrayList:

dynamicArray.add(10); dynamicArray.add(12); dynamicArray.add(20);

Note: TheArrayListonly stores objects so although the above lines appear to add int values toArrayListthe are automatically changed toIntegerobjects as they are appended to theArrayList.

A standard array can be used to populate anArrayListby converted it to a List collection using the Arrays.asList method and adding it to theArrayListusing theaddAllmethod:

String names = {"Bob", "George", "Henry", "Declan", "Peter", "Steven"}; ArrayList dynamicStringArray = new ArrayList(20); dynamicStringArray.addAll(Arrays.asList(names));

One thing to note aboutArrayListis the elements don't have to be of the same object type. Even though thedynamicStringArrayhas been populated by String objects, it still can accept number values:


To minimize the chance of errors it's best to specify the type of objects you want theArrayListto contain. This can be done at the creation stage by using generics:

ArrayList dynamicStringArray = new ArrayList(20);

Now the if we try to add an object that isn't aStringa compile-time error will be produced.

Displaying the Items in an ArrayList

To display the items in anArrayListthetoStringmethod can be used:

System.out.println("Contents of the dynamicStringArray: " + dynamicStringArray.toString());

which results in:

Contents of the dynamicStringArray: Bob, George, Henry, Declan, Peter, Steven

Inserting an Item into the ArrayList

An object can be inserted anywhere into theArrayListindex of elements by using the add method and passing the position for the insertion. To add theString "Max"to thedynamicStringArrayat position 3:

dynamicStringArray.add(3, "Max");

which results in (don't forget the index of anArrayListstarts at 0):

Bob, George, Henry, Max, Declan, Peter, Steven

Removing an Item from an ArrayList

Theremovemethod can be used to remove elements from theArrayList. This can be done in two ways. The first is to supply the index position of the element to be removed:


theString "Henry"in postion 2 has been removed:

Bob, George, Max, Declan, Peter, Steven

The second is to supply the object to be removed. This will remove the first instance of the object. To remove "Max" from thedynamicStringArray:


TheString "Max"is no longer in theArrayList:

Bob, George, Declan, Peter, Steven

Replacing an Item in an ArrayList

Rather than removing an element and inserting a new one in its place thesetmethod can be used to replace an element in one go. Just pass the index of the element to be replaced and the object to replace it with. To replace "Peter" with "Paul":


which results in:

Bob, George, Declan, Paul, Steven

Other Useful Methods

There are a number of useful methods to help navigate the contents of an arraylist:

  • The number of elements contained within anArrayListcan be found using thesizemethod:

    System.out.println("There are now " + dynamicStringArray.size() + " elements in the ArrayList");After all our manipulations ofdynamicStringArraywe're down to 5 elements:

    • There are now 5 elements in the ArrayList

  • Use theindexOfmethod to find the index position of a particular element:

    System.out.println("The index position of George is : " + dynamicStringArray.indexOf("George"));TheString "George"is in index position 1:

    • The index position of George is : 1

  • To clear all the elements from anArrayListthe clear method is used:


  • Sometimes it can be useful to see if theArrayListhas any elements at all. Use theisEmptymethod:

    System.out.println("Is the dynamicStringArray empty? " + dynamicStringArray.isEmpty());which afterclearmethod call above is now true:

    • Is the dynamicStringArray empty? true