Fiveable

💻AP Computer Science A Unit 4 Review

QR code for AP Computer Science A practice questions

4.8 ArrayList Methods

4.8 ArrayList Methods

Written by the Fiveable Content Team • Last updated June 2026
Verified for the 2027 exam
Verified for the 2027 examWritten by the Fiveable Content Team • Last updated June 2026
💻AP Computer Science A
Unit & Topic Study Guides

Frequently Asked Questions

Previous Exam Prep

Study Tools

Exam Skills

AP Cram Sessions 2021

Pep mascot

What ArrayList methods are on the AP CSA exam?

ArrayList methods let you build and change a list whose size can grow or shrink while your program runs. For AP Computer Science A you mainly need the methods on the Java Quick Reference: size, both versions of add, get, set, and remove. Knowing exactly what each one returns and how it shifts other elements is the key to tracing and writing correct code.

Why This Matters for the AP Computer Science A Exam

ArrayList shows up on both the multiple-choice and free-response sections. You will trace short code segments to predict output, explain why a segment will not compile or work as intended, and write code that stores and manipulates collections of objects. One of the free-response questions centers on analyzing an ArrayList of objects from a given class, where you often insert or remove elements and adjust your loop to handle shifting indices. Getting comfortable with the exact behavior of each ArrayList method now pays off across the whole exam.

Only the methods listed on the Java Quick Reference are fair game on the exam. That means contains, indexOf, and isEmpty are not part of the required AP method set, even though they exist in real Java. Build your fluency around the methods the exam will actually test.

Key Takeaways

  • An ArrayList has a dynamic size and stores object references, so it can grow and shrink while arrays cannot.
  • The AP method set is size(), add(E obj), add(int index, E obj), get(int index), set(int index, E obj), and remove(int index).
  • add(int index, E obj) shifts elements right and remove(int index) shifts elements left, which changes the indices of later elements.
  • set and remove return the element that used to be there; add(E obj) returns true; add(int index, E obj) returns nothing.
  • Use ArrayList<E> with a type, like ArrayList<String>, so the compiler catches type errors early.
  • Valid indices run from 0 to size() - 1; going outside that range throws an IndexOutOfBoundsException.

Setting Up an ArrayList

ArrayList lives in the java.util package, so you need an import statement before you use it:

</>Java
import java.util.ArrayList;

The no-argument constructor builds an empty list. Always specify the element type with ArrayList<E>:

</>Java
ArrayList<String> names = new ArrayList<String>();

Writing ArrayList<String> instead of plain ArrayList lets the compiler find type errors while you write code, instead of letting them surface as run-time errors. The type parameter E stands for whatever element type you choose, so the parameter and return types of the methods match that type.

Because an ArrayList stores object references, the element type must be a class type. To store numbers you use the wrapper classes, such as ArrayList<Integer> or ArrayList<Double>.

The Required ArrayList Methods

These six methods are the ones on the Java Quick Reference, so they are the ones the exam expects you to know.

MethodWhat it doesReturns
int size()Number of elements in the listThe count as an int
boolean add(E obj)Appends obj to the end of the listtrue
void add(int index, E obj)Inserts obj at index, shifting later elements rightnothing
E get(int index)Returns the element at indexThe element
E set(int index, E obj)Replaces the element at index with objThe element formerly there
E remove(int index)Removes the element at index, shifting later elements leftThe element formerly there

For add(int index, E obj), the index can be anywhere from 0 up to size(), inclusive. Adding at size() puts the element at the end. Inserting anywhere else moves the element currently at that position, and everything after it, one spot to the right, and the size goes up by 1.

For remove(int index), everything after the removed element slides one spot to the left, and the size goes down by 1.

Adding Elements

</>Java
import java.util.ArrayList;

public class AddingMethods {
    public static void main(String[] args) {
        ArrayList<String> playlist = new ArrayList<String>();

        // add(E obj) - appends to the end
        playlist.add("Song A");
        playlist.add("Song B");
        playlist.add("Song C");
        System.out.println("After basic adds: " + playlist);

        // add(int index, E obj) - inserts and shifts later elements right
        playlist.add(1, "New Song");
        System.out.println("After insertion: " + playlist);
    }
}

add("Song A") always lands at the end. add(1, "New Song") puts the new value at index 1 and shifts "Song B" and "Song C" to the right.

Accessing and Replacing Elements

</>Java
import java.util.ArrayList;

public class GradeManager {
    public static void main(String[] args) {
        ArrayList<Integer> grades = new ArrayList<Integer>();
        grades.add(85);
        grades.add(92);
        grades.add(78);
        grades.add(96);

        // get(int index) - reads without changing the list
        int firstGrade = grades.get(0);
        System.out.println("First grade: " + firstGrade);

        // set(int index, E obj) - replaces and returns the old value
        int oldGrade = grades.set(2, 88);  // changes 78 to 88
        System.out.println("Replaced " + oldGrade + " with 88");
        System.out.println("Updated grades: " + grades);

        // size() - how many elements
        System.out.println("Total grades: " + grades.size());
    }
}

get and size only read information, so they never change the list. set changes one element and hands back the value that used to be there, which is handy when you want to know what you replaced.

Removing Elements

</>Java
import java.util.ArrayList;

public class CartExample {
    public static void main(String[] args) {
        ArrayList<String> cart = new ArrayList<String>();
        cart.add("Apples");
        cart.add("Bread");
        cart.add("Milk");
        cart.add("Cookies");

        System.out.println("Original cart: " + cart);

        // remove(int index) - removes by position and returns that element
        String removedItem = cart.remove(0);
        System.out.println("Removed: " + removedItem);
        System.out.println("Cart after removal: " + cart);
        System.out.println("New size: " + cart.size());
    }
}

remove(0) takes out "Apples" and returns it, then slides "Bread", "Milk", and "Cookies" left so they now sit at indices 0, 1, and 2.

How to Use This on the AP Computer Science A Exam

Code Tracing

When you trace ArrayList code, keep a running picture of the list and its indices after every call. Write the list out and update it line by line.

  • After add(int index, E obj), every element from that index onward moves up one index.
  • After remove(int index), every later element moves down one index.
  • Remember that set and remove return values you can store or print, while add(E obj) returns true and add(int index, E obj) returns nothing.

Free Response

For free-response code writing that uses an ArrayList of objects, you will often insert or delete elements while looping. When you remove an element, the next element slides into the current index, so a normal counter would skip it. Adjust your loop so you do not move past the element that just shifted into place. Be sure your indices stay between 0 and size() - 1 so you avoid an IndexOutOfBoundsException.

Common Trap

The exam frequently tests the difference between treating a number as an index and treating it as a value. Since the required remove method takes an int index, a call like remove(1) removes the element at index 1. Read each call carefully and confirm whether the argument is a position or something else.

Common Misconceptions

  • remove(int index) removes the element at that index, not the value equal to that number. With ArrayList<Integer>, remove(1) removes whatever is at index 1.
  • add(E obj) and add(int index, E obj) do not return the same thing. The append version returns true; the insert version returns nothing.
  • Inserting or removing in the middle of a list is not free. Other elements shift, and their indices change, which is exactly what trips people up in loops.
  • The valid indices are 0 through size() - 1. Using size() itself as a read index throws an IndexOutOfBoundsException, even though it is a legal position for add(int index, E obj).
  • contains, indexOf, and isEmpty are real Java methods but are not in the AP required method set, so do not rely on them in your exam answers.
  • Declaring ArrayList<String> names; does not create a list. You must assign new ArrayList<String>() before calling methods, or you get a NullPointerException.

Vocabulary

The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.

Term

Definition

add(E obj) method

An ArrayList method that appends an object to the end of the list and returns true.

add(int index, E obj) method

An ArrayList method that inserts an object at a specified position and shifts elements to the right.

ArrayList

A resizable array implementation in Java that can dynamically grow or shrink to store a collection of objects.

generic type

A parameterized type that specifies the data type of elements a collection will contain, such as ArrayList<E>.

get(int index) method

An ArrayList method that returns the element at a specified position in the list.

import statement

A Java statement used to make classes from a package available for use in a program.

index

A numeric position in a string, starting from 0 for the first character and going up to one less than the length of the string.

java.util package

A Java library package that contains utility classes including ArrayList.

mutable

Capable of being changed or modified after creation, such as an ArrayList that can grow or shrink in size.

object reference

A value that points to the memory location where an object is stored, allowing access to that object.

remove(int index) method

An ArrayList method that removes the element at a specified position and shifts elements to the left.

set(int index, E obj) method

An ArrayList method that replaces the element at a specified position and returns the former element.

size() method

An ArrayList method that returns the number of elements currently in the list.

type parameter

A placeholder for a specific data type in a generic class, represented by E in ArrayList<E>.

Frequently Asked Questions

What ArrayList methods are on the AP CSA exam?

The AP CSA Java Quick Reference includes size(), add(E obj), add(int index, E obj), get(int index), set(int index, E obj), and remove(int index). You should know what each method does, what it returns, and how it affects indices.

What does size() do for an ArrayList?

size() returns the number of elements currently in the ArrayList as an int. Because ArrayList indices start at 0, the last valid read index is size() - 1.

What is the difference between add(E obj) and add(int index, E obj)?

add(E obj) appends an element to the end of the list and returns true. add(int index, E obj) inserts an element at a specific position, shifts later elements right, increases the size by 1, and returns nothing.

What do get, set, and remove return in an ArrayList?

get(int index) returns the element at that index without changing the list. set(int index, E obj) replaces an element and returns the old element. remove(int index) removes an element, shifts later elements left, and returns the removed element.

Why do ArrayList indices shift after insertions and removals?

ArrayLists keep elements in order. When you insert at an index, elements at that index and after it shift right. When you remove at an index, later elements shift left. This matters when tracing loops or writing FRQ code.

How is ArrayList tested on AP Computer Science A FRQs?

ArrayList often appears in code-writing FRQs with collections of objects. You may need to traverse a list, call required methods, insert or remove elements safely, and adjust loop indices so shifted elements are not skipped.

Pep mascot
Upgrade your Fiveable account to print any study guide

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Click below to go to billing portal → update your plan → choose Yearly→ and select "Fiveable Share Plan". Only pay the difference

Plan is open to all students, teachers, parents, etc
Pep mascot
Upgrade your Fiveable account to export vocabulary

Download study guides as beautiful PDFs See example

Print or share PDFs with your students

Always prints our latest, updated content

Mark up and annotate as you study

Plan is open to all students, teachers, parents, etc
report an error
description

screenshots help us find and fix the issue faster (optional)

add screenshot