7.1. Intro to ArrayLists¶
In the last unit, we learned about using arrays to hold collections of related data. However arrays are not very flexible. Most notably, the size of an array is established at the time of creation and cannot be changed. What if you don’t know how big the collection of data will be? What if you want to both add and remove items from a collection? For example, if you wanted to represent a shopping list, you might add to the list throughout the week and remove things from the list while you are shopping. You probably would not know how many items will be on the list at the beginning of the week.
For cases like this, Java has a class called
ArrayList which is a re-sizable
list. It is called
ArrayList because it stores the items that have been
added to it in an underlying array. But it also takes care of keeping track of
how many items have been added to the array and it will create a new bigger
array under the covers when needed to hold more items.
You can use
ArrayList instead of arrays whenever you don’t know the size of
the array you need or you know that you will add and remove items and may need
to change the array’s size dynamically during run time. An
mutable, meaning it can change during run time by adding and removing objects
ArrayList is often called just a list on the CSA exam. Prior to 2020
the AP CSA curriculum included interfaces which are somewhat like classes
and the interface
List was often used to declare a variable that would
refer to an
ArrayList. Interfaces are no longer on the exam, but if you
List being used in an old exam question just assume it’s an
7.1.1. Packages and imports¶
ArrayList class is in the
java.util package. A package is a set
or library of related classes. The classes we have used until now, such as
Math, are in the special package
java.lang whose classes
are always available in any Java program. Other packages, such as
provide classes that can only be used either by importing them or (much more
rarely) by referring to them by their full name which includes the package as a
prefix. The full name of
ArrayList is thus
rather than type that out all the time, in any class where we want to use
ArrayList we will usually import it with an
Import statements have to come before the class definition in a Java source file
and serve to tell Java which class you mean when you use a short name like
ArrayList. To import just one class we use a single
import of the
fully-qualified name of the class like this:
// Import just the ArrayList class from java.util
After such an import statement, anywhere
ArrayList is used as a class name
in the file it will be taken to mean
Another option is to import all the classes in a package with a “wildcard” import:
// Import everything in java.util including ArrayList
This import statement will also cause,
ArrayList to refer
java.util.ArrayList. But many other names of classes defined in the
java.util package will also be available whether you use them or not. (One
that you have probably used by now is
Scanner which can be used to read
input a user types at the command line.) Using wildcard imports can cause
conflicts if you import all the classes from two different packages and they
have class names in common but usually that’s not a problem, at least with
packages that are part of Java itself.
Don’t worry about adding import statements on the AP CSA exam. Any that you need will be provided for you.
7.1.2. Declaring and Creating ArrayLists¶
To declare a ArrayList use
ArrayList<Type> name where Type, called a
type parameter is the type of objects you want to store in the ArrayList.
For example a variable naming an
ArrayList meant to hold
ArrayList<String> as shown in the code below. You can declare a
variable to just be of type
ArrayList, with no type parameter, and it’ll be
approximately the same as if you had declared
ArrayList<Object>, but it is
good practice to specify the type of objects you intend to store in an
ArrayList as it allows the compiler to find errors that would otherwise be
missed until run time.
// ArrayList<Type> name = new ArrayList<Type>();
// An ArrayList of Strings:
ArrayList<String> shoppingList = new ArrayList<String>();
ArrayLists can only hold reference types like
String. Since they
can’t hold primitive types like
double, if we want a
collection of numbers we need to use the wrapper classes
Double. However, because of autoboxing, if you declare an
ArrayList<Double> you can mostly treat the
elements of the
ArrayList as if they were in fact
In the code below we are declaring a variable called
nameList that can
refer to a
ArrayList of strings, but currently doesn’t refer to any
ArrayList yet as it’s set to
As with other reference types, declaring a
ArrayList variable doesn’t
actually create a
ArrayList object. It only creates a variable that can
refer to a
null. To actually create a
ArrayList we must
invoke a constructor such as
You can get the number of items in a
ArrayList using the
Notice that a newly constructed
ArrayList is empty and thus has a size of 0.
Also remember that you can’t call methods on
null so trying to call
on the value of
list2 at line 10 below causes a
The following code demonstrates a NullPointerException. Change the list2 declaration so that it creates a new ArrayList to remove the NullPointerException.
You can also create ArrayLists of integer and double values. However, you have
Double as the type parameter because
can only hold objects, not primitive values. All primitive types must be
wrapped in objects before they are added to an ArrayList. For example,
int values can be wrapped in
double values can be
Double objects. However this normally happens automatically
thanks to autoboxing.
You can actually put in any kind of objects in an
instances of classes that you write, such as the
Pet classes from Unit 5.
Here’s an example of a Integer ArrayList.
Although it is not on the AP exam, you can convert an array to a
the static method
asList from the
Arrays helper class:
Arrays.asList(arrayname). Note that
ArrayList has a
that is automatically called to print the list in a nice format.
Example code creating an ArrayList from an array.
You can add values to an
ArrayList using its add method, described in
detail in the next lesson. Try the code below. Note that the type of the
Integer, also determines the type of parameters
and return types for all of its methods, so add and print work for any type of
ArrayList. And when the
ArrayList is a list of
autoboxing takes care of wrapping the
int arguments like
into instances of
Integer for us.
Can you add another item to the shopping list?
7.1.3. Programming Challenge : FRQ Digits¶
This programming challenge is based on the 2017 Free Response Question part 1a on the 2017 AP CSA
exam. In this question, you are asked to write a constructor for a class called
Digits. This constructor takes an integer number as its argument and divides
it up into its digits and puts the digits into an
ArrayList. For example,
new Digits(154) creates an ArrayList with the digits [1, 5, 4].
First, let’s discuss how to break up a number into its digits. Try the code
below. What happens if you divide an integer by 10? Remember that in integer
division the result truncates (cuts off) everything to the right of the decimal
point. Which digit can you get by using
% 10 which returns the remainder
after dividing by 10? Try a different number and guess what it will print and
then run to check.
Set number to a different number and guess what number / and % will return. Which operator gives you a digit in number?
We can use a while loop to print out each digit in reverse order starting from the right (4, 5, 1 for the number 154) while dividing it by 10. You can try it in the active code above. Here is the pseudocode:
while number is greater than 0
print out the last digit using %
change the number to cut off the last digit using /
Now, let’s write a constructor for the
Digits class that uses this loop and
adds each found digit to the
ArrayList instead of printing it out. You can
use a special method called
Collections.reverse(digitsList); to reverse the
order of the digits in the
ArrayList after the loop to get them in the right
order. In the next lesson, we will also learn how to use a different
method that adds in elements at any index instead of the end.
Complete the challenge below to put the digits of a number in an ArrayList.
ArrayLists are re-sizable lists that allow adding and removing items to change their size during run time.
ArrayListclass is in the
java.utilpackage. You must import
java.util.*to use it.
ArrayListobject contains object references and is mutable, meaning it can change (by adding and removing items from it).
ArrayList()constructs an empty list of size 0.
Java allows the generic type
ArrayList<E>, where the generic type
Especifies the type of the elements, like
Integer. Without it, the type will be
ArrayList<E>is preferred over
ArrayListbecause it allows the compiler to find errors that would otherwise be found at run time.
ArrayList<E>is specified, the types of the reference parameters and return type when using its methods are type
ArrayLists cannot hold primitive types like
double, so you must use the wrapper classes
Doubleto put numerical values into an
ArrayList. However autoboxing usually takes care of that for you.