Using the ArrayList in Java

Male office worker with laptop
Michael Bodmann/E+/Getty Images

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:

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

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);
dynamicStringArray.addAll(Arrays.asList(names));

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:

dynamicStringArray.add(456);

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 = new ArrayList(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:

dynamicStringArray.remove(2);

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:

dynamicStringArray.remove("Max");

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":

dynamicStringArray.set(3,"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:
    dynamicStringArray.clear();
  • 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
Format
mla apa chicago
Your Citation
Leahy, Paul. "Using the ArrayList in Java." ThoughtCo, Aug. 26, 2020, thoughtco.com/using-the-arraylist-2034204. Leahy, Paul. (2020, August 26). Using the ArrayList in Java. Retrieved from https://www.thoughtco.com/using-the-arraylist-2034204 Leahy, Paul. "Using the ArrayList in Java." ThoughtCo. https://www.thoughtco.com/using-the-arraylist-2034204 (accessed March 28, 2024).