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), andremove(int index). add(int index, E obj)shifts elements right andremove(int index)shifts elements left, which changes the indices of later elements.setandremovereturn the element that used to be there;add(E obj)returnstrue;add(int index, E obj)returns nothing.- Use
ArrayList<E>with a type, likeArrayList<String>, so the compiler catches type errors early. - Valid indices run from 0 to
size() - 1; going outside that range throws anIndexOutOfBoundsException.
Setting Up an ArrayList
ArrayList lives in the java.util package, so you need an import statement before you use it:
</>Javaimport java.util.ArrayList;
The no-argument constructor builds an empty list. Always specify the element type with ArrayList<E>:
</>JavaArrayList<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.
| Method | What it does | Returns |
|---|---|---|
int size() | Number of elements in the list | The count as an int |
boolean add(E obj) | Appends obj to the end of the list | true |
void add(int index, E obj) | Inserts obj at index, shifting later elements right | nothing |
E get(int index) | Returns the element at index | The element |
E set(int index, E obj) | Replaces the element at index with obj | The element formerly there |
E remove(int index) | Removes the element at index, shifting later elements left | The 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
</>Javaimport 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
</>Javaimport 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
</>Javaimport 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
setandremovereturn values you can store or print, whileadd(E obj)returnstrueandadd(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. WithArrayList<Integer>,remove(1)removes whatever is at index 1.add(E obj)andadd(int index, E obj)do not return the same thing. The append version returnstrue; 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. Usingsize()itself as a read index throws anIndexOutOfBoundsException, even though it is a legal position foradd(int index, E obj). contains,indexOf, andisEmptyare 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 assignnew ArrayList<String>()before calling methods, or you get aNullPointerException.
Related AP Computer Science A Guides
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.