1. Computing
Send to a Friend via Email

Using the ArrayList


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 the ArrayList class. The ArrayList class provides the means to make dynamic arrays (i.e., their length can increase and decrease).

Import Statement

import java.util.ArrayList;

Create an ArrayList

An ArrayList can be created using the simple constructor:

ArrayList dynamicArray = new ArrayList();

This will create an ArrayList with an initial capacity for ten elements. If a larger (or smaller) ArrayList is 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 the ArrayList:


Note: The ArrayList only stores objects so although the above lines appear to add int values to ArrayList the are automatically changed to Integer objects as they are appended to the ArrayList.

A standard array can be used to populate an ArrayList by converted it to a List collection using the Arrays.asList method and adding it to the ArrayList using the addAll method:

String[] names = {"Bob", "George", "Henry", "Declan", "Peter", "Steven"};
ArrayList dynamicStringArray = new ArrayList(20);

One thing to note about ArrayList is the elements don't have to be of the same object type. Even though the dynamicStringArray has 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 the ArrayList to contain. This can be done at the creation stage by using generics:

ArrayList dynamicStringArray<String> = new ArrayList<String>(20);

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

Displaying the Items in an ArrayList

To display the items in an ArrayList the toString method 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 the ArrayList index of elements by using the add method and passing the position for the insertion. To add the String "Max" to the dynamicStringArray at position 3:

dynamicStringArray.add(3, "Max");

which results in (don't forget the index of an ArrayList starts at 0):

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

Removing an Item from an ArrayList

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


the String "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 the dynamicStringArray:


The String "Max" is no longer in the ArrayList:

[Bob, George, Declan, Peter, Steven]

Replacing an Item in an ArrayList

Rather than removing an element and inserting a new one in its place the set method 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 an ArrayList can be found using the size method:
    System.out.println("There are now " + dynamicStringArray.size() + " elements in the ArrayList");

    After all our manipulations of dynamicStringArray we're down to 5 elements:

    There are now 5 elements in the ArrayList
  • Use the indexOf method to find the index position of a particular element:
    System.out.println("The index position of George is : " + dynamicStringArray.indexOf("George"));

    The String "George" is in index position 1:

    The index position of George is : 1
  • To clear all the elements from an ArrayList the clear method is used:
  • Sometimes it can be useful to see if the ArrayList has any elements at all. Use the isEmpty method:
    System.out.println("Is the dynamicStringArray empty? " + dynamicStringArray.isEmpty());

    which after clear method call above is now true:

    Is the dynamicStringArray empty? true
  1. About.com
  2. Computing
  3. Java
  4. Java Syntax
  5. Utilizing the Java API
  6. java.util
  7. Using the ArrayList

©2014 About.com. All rights reserved.