Please enable JavaScript to use CodeHS

Chapter 7

ArrayLists

7.1 ArrayLists

In the last unit, you learned how to store data more efficiently using arrays. Arrays are able to store a list of values of the same type in one variable, making it easier to store and keep track of data that you need for your programs. One of the limitations of using arrays to store data is the fact that they have a fixed number of items that can be added or changed. Elements can only be added to an array by creating a new array of a different size and copying the data from the initial array to the new one:

int[] setSize = new int[5];
int[] newSize = new int[6];
setSize = newSize;
Java

This process is tedious, and requires a high degree of difficulty, especially if the list being used is going to change in size often. A more convenient way to create adjustable arrays is by using the ArrayList class.

Introducing ArrayLists

ArrayLists are similar to arrays, except that they are a mutable list of object references, meaning that the size of the list can be adjusted freely. Creating an ArrayList is as simple as initializing a new ArrayList in a program:

import java.util.ArrayList;

public static void main(String[] args)
{
  ArrayList<E> list = new ArrayList<E>();
}
Java

In order to use the ArrayList class, the ArrayList class needs to be imported from the java util package. This can be done by writing import java.util.ArrayList at the top of the class file.

ArrayList objects are created in the same fashion as other object classes. The primary difference with ArrayLists is that the element type of the ArrayList must be specified using angled bracket <>. In this example, E represents the data type that will be used in the ArrayList. This can be replaced by an object data type:

ArrayList<String> list = new ArrayList<String>();
Java

The declaration type for an ArrayList must match the initialization type. In this case, both types reference the String data type.

When this line of code is executed, the ArrayList list will store an empty ArrayList with no index values. Unlike arrays, the number of items that will be stored in an ArrayList does not need to be specified. Values can be added to the ArrayList after the ArrayList is initialized. This will be explored more in the next chapter.

Initializing ArrayLists without Types

ArrayLists can actually be declared without specifying the type that will be included in the ArrayList:

ArrayList list = new ArrayList();
Java

While this appears to be a simpler initialization process, specifying the data type is highly recommended because it allows the compiler to find errors before run time. This makes it easier to find errors, and program more efficiently, as the program will indicate if there is an issue before any code is run.

ArrayList vs Array

While arrays and ArrayLists are fairly similar data structures, the key differences between them lie in their creation, mutability, and the types of data that they can store:

Feature Array ArrayList
Create int[] arr = new int[5] ArrayList list = new ArrayList();
Mutability Fixed size Changing size
Types Can store Primitives or Objects Can only store Objects, handles autoboxing and unboxing
When to Use With data sets that don’t change in value or size frequently With data that changes in value or size frequently

As noted here, arrays are a fixed size, whereas ArrayLists can contract and expand. ArrayLists are only able to store objects, whereas arrays can store both primitive and object values. Foruntuantley, autoboxing and unboxing allows you to send many primitive variables into an ArrayList, but the ArrayList still needs to be set up using the object type.

When choosing to use a data structure, it’s important to know if the data being stored should be a fixed, or unfixed size in length. Which data structure you choose will depend on how frequently that data needs to be updated or accessed.

Initializing an ArrayList

Array vs. ArrayList Initialization

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    How do you create an ArrayList of integers?

Exercise: Initializing an ArrayList

A veternarian is interested in keeping track of the dogs it sees a little more easily.

Help them out by creating two ArrayLists:

  1. Create an ArrayList called dogName that stores an ArrayList of all the dog names.
  2. Create an Integer ArrayList called dogAge that stores the age of the dogs.