1. Reference Guide

Key Topics


Term Definition Example
ArrayList A list of objects that grows and shrinks. ArrayList<String> names = new ArrayList<String>();
add Appends to the end, or inserts at an index. names.add("Ana"); names.add(0, "Bo");
get Reads the element at an index. names.get(0)
set Replaces the element at an index, returns the old one. names.set(1, "Cy");
remove Deletes by index or by value, shifting later elements left. names.remove(0);
size Number of elements right now. names.size()
  • Valid indexes run from 0 to size() - 1. Anything else throws IndexOutOfBoundsException.
  • An ArrayList holds objects, so use ArrayList<Integer>, never ArrayList<int>.

Array vs. ArrayList

  Array ArrayList
Size Fixed Grows and shrinks
Count arr.length list.size()
Read / write arr[i] / arr[i] = v list.get(i) / list.set(i, v)

Picking a remove

  • Remove by position → nums.remove(2)
  • Remove by value → nums.remove(Integer.valueOf(2))

2. LxD Cycle Process

Empathize: Students coming from arrays write list[2] and list.length, which do not compile. The bigger trap is silent: nums.remove(1) removes index 1, not the value 1, so the wrong element disappears with no error.

Define:

  • POV: CSA students need each ArrayList method mapped onto the array syntax they already know, because every mismatch is either a compiler error or a silent bug.
  • Learning Goal: Students will build and change an ArrayList with add, get, set, remove, and size, and choose the right remove.

Ideate:

  • HMW Question: How might we make remove(1) versus remove(Integer.valueOf(1)) visible before it causes a bug?
  • Activity: Build a playlist in the popcorn hack, pick the right method in the MCQ, then write two list tools.

Prototype:

  • A reference guide, runnable Java examples, a personalized popcorn hack, an MCQ knowledge check, and a scaffolded grade rubric.
  • Excellence means explaining which remove runs and why, and using size() instead of hardcoded numbers.

Test:

  • Ask peers in peer review to complete the practice without additional explanation.
  • Observe whether peers use size() and pick the right remove.
  • On submission, collect evidence from runner output, MCQ results, AI grading and student explanations.
  • After teaching, grading and analysis, come back and revise lesson to complete teaching cycle for continuous improvement.

3. College Board Requirements

AP CSA Unit 4, Topic 4.8 ArrayList Methods. Quoted from the course and exam description (College Board, 2025, pp. 110-111):

  • 4.8.A.1 “An ArrayList object is mutable in size and contains object references.”
  • 4.8.A.3 “ArrayList<E> is preferred over ArrayList. For example, ArrayList<String> names = new ArrayList<String>(); allows the compiler to find errors that would otherwise be found at run-time.”
  • 4.8.A.6 “The indices for an ArrayList start at 0 and end at the number of elements - 1.”

The methods in the Key Topics table are the ones on the Java Quick Reference (EK 4.8.A.5).


4. Lesson Plan

Learning Objective: Build and change an ArrayList with add, get, set, remove, and size.

Success Criteria: You can predict a list after each method call, and choose remove(int index) or remove(Object obj) on purpose.

Tech Talk (5 minutes)

An array is a row of fixed boxes. An ArrayList is a grocery list: it grows when you add and shrinks when you cross something off.

Code Runner Challenge

Predict each line before running, then change get(3) to get(2)

View IPYNB Source
ArrayList<String> groceries = new ArrayList<String>();
groceries.add("Milk");            // [Milk]
groceries.add(0, "Eggs");         // [Eggs, Milk]
groceries.set(1, "Oat Milk");     // [Eggs, Oat Milk]
groceries.remove(0);              // [Oat Milk]
groceries.size();                 // 1
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

5. Code Examples

A. List basics. The ListBasics class adds, reads, replaces, and counts

Array habits that break:


scores[1] = 95;        // does not compile, use scores.set(1, 95)
scores.length          // does not compile, use scores.size()

Terminology:

get and set never change the size. Only add and remove do.


Code Runner Challenge

Predict both lists before running, then try removing Integer.valueOf(9)

View IPYNB Source
// CODE_RUNNER: Predict each line before running, then change get(3) to get(2)
// add, get, set, size
import java.util.ArrayList;

public class ListBasics {
    public static void main(String[] args) {
        ArrayList<Integer> scores = new ArrayList<Integer>();
        scores.add(70);
        scores.add(90);
        scores.add(1, 85);                 // insert, 90 shifts right

        System.out.println(scores + ", size " + scores.size());
        int old = scores.set(1, 95);       // returns the replaced value
        System.out.println("replaced " + old + " -> " + scores);

        try {
            System.out.println(scores.get(3));
        } catch (IndexOutOfBoundsException e) {
            System.out.println("get(3) failed: " + e.getMessage());
        }
    }
}

ListBasics.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

B. Two remove methods. The RemoveComparison class removes by position and by value

Common remove mistake:


nums.remove(1);                    // int: removes the element AT index 1
nums.remove(Integer.valueOf(1));   // Integer: removes the VALUE 1

Terminology:

Java picks the method from the argument type. An int means index; an Integer means value.


Code Runner Challenge

Replace the sample values with your own, then run it

View IPYNB Source
// CODE_RUNNER: Predict both lists before running, then try removing Integer.valueOf(9)
// remove(int index) vs. remove(Object obj)
import java.util.ArrayList;

public class RemoveComparison {
    public static void main(String[] args) {
        ArrayList<Integer> byIndex = new ArrayList<Integer>();
        byIndex.add(1);
        byIndex.add(2);
        byIndex.add(3);
        byIndex.remove(1);
        System.out.println("byIndex = " + byIndex);

        ArrayList<Integer> byValue = new ArrayList<Integer>();
        byValue.add(1);
        byValue.add(2);
        byValue.add(3);
        byValue.remove(Integer.valueOf(1));
        System.out.println("byValue = " + byValue);
    }
}

RemoveComparison.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

6. Hacks & Practice Tasks

Prepare your submission IPYNB

  1. Create a new notebook in your portfolio homework area: _notebooks/homework.
  2. Add one raw cell at the top with the frontmatter:

Code Runner Challenge

Fill in the blanks, then run it

View IPYNB Source
---
layout: post
codemirror: true
title: ArrayList Methods HW
categories: [Java]
lesson_language: Java
lesson_topic: ArrayList Methods HW
lesson_part: interactive
lesson_type: lesson
permalink: /csa/unit_04/4_8-hw
author: yourGithubID
---
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...
  1. Add code cells for the Popcorn Hack and the Homework Hack. Make sure every cell runs with visible output.
  2. Submit the link to your published page at the bottom of this page, and paste this in the description box:
Lesson: CSA 4.8 ArrayList Methods
MCQ 4.8: <paste the copied result line, such as 3/4 | answers: B,C,B,D>
Popcorn: playlist uses add, add(index), set, remove, size (yes/no)
Homework: swapFirstAndLast output = <output>
Homework: removeFirstOccurrence output = <output>

Submission Safety Rules (Read First)

  • One class per cell, ending with ClassName.main(null);.
  • Run each cell and leave the output showing.
  • Use your own values, not the sample answer.
  • Include your MCQ score.
  • Use ## headings or smaller.

Popcorn Hack (In-Class)

2-minute challenge: build a playlist, then run the cell.

  1. Add three songs
  2. Insert a favorite at index 0
  3. Replace the last song (use size() - 1)
  4. Remove one song, then print the list and its size

Replace the sample values with your own.

// CODE_RUNNER: Replace the sample values with your own, then run it
// Practice #1 - Try it yourself first!
import java.util.ArrayList;

public class MyPlaylist {
    public static void main(String[] args) {
        // Try writing your own code here first!

        // Sample answer:
        ArrayList<String> songs = new ArrayList<String>();
        songs.add("Golden Hour");
        songs.add("Levitating");
        songs.add("Sunflower");
        songs.add(0, "Espresso");
        songs.set(songs.size() - 1, "Heat Waves");
        songs.remove("Levitating");

        System.out.println(songs + ", " + songs.size() + " songs");
    }
}

MyPlaylist.main(null);

MCQ Check

4 questions, one at a time. Answer, check, then go to the next one. At the end, copy the score line into your submission notes.

Homework Hack

Task: Write two list tools. swapFirstAndLast swaps the first and last values in place using get, set, and a temporary variable. removeFirstOccurrence removes the first element equal to target using remove(Object obj).

Solution Skeleton:

// CODE_RUNNER: Fill in the blanks, then run it
import java.util.ArrayList;

public class ListTools {
    // 1. Save index 0 in a temp variable, then use get() and set() with size() - 1
    public static void swapFirstAndLast(ArrayList<Integer> nums) {
    }

    // 2. Box target so Java calls remove(Object obj), not remove(int index)
    public static void removeFirstOccurrence(ArrayList<Integer> nums, int target) {
    }

    public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<Integer>();
        nums.add(1); nums.add(7); nums.add(3); nums.add(7);

        swapFirstAndLast(nums);
        System.out.println(nums);   // expect [7, 7, 3, 1]

        removeFirstOccurrence(nums, 7);
        System.out.println(nums);   // expect [7, 3, 1]
    }
}

ListTools.main(null);

Grading Plan (1 Point Total)

Part Points What earns the points
Popcorn 0.2 Playlist uses add, add(index, obj), set, remove, and size, and the cell runs.
MCQ 0.2 4 correct. 0.15 for 2 or 3, 0.1 if every question was answered.
Homework: swap 0.15 First and last values swapped in place.
Homework: temp variable 0.15 Uses get, set, and a temporary variable, with size() - 1 for the last index.
Homework: remove by value 0.15 Uses remove(Object obj), not remove(int index).
Homework: output 0.15 Both printed lists match the expected output.
Total 1.0  

Quick Validation Checklist

  • Each cell ends with ClassName.main(null); and shows output.
  • MCQ score in the notes.
  • size() used, never length.
  • Integer.valueOf(target) passed to remove.

7. Lesson Revisions

Revision Made: Changed the remove example from [10, 20, 30] to [1, 2, 3] so remove(1) and remove(Integer.valueOf(1)) give different results side by side. Cut the code examples from four to two and moved the length vs size() mistake into Example A. Added the ArrayList<int> MCQ question. Shortened the homework from three methods to two and gave the swap test a list where the first and last values differ.


8. Feedback Evidence

Feedback Received: In the practice run, 4 of 6 peers predicted that values.remove(1) on [10, 20, 30] would “remove the 1,” and two said the old example did not help because both lists printed [10, 30]. One peer tried ArrayList<int> in the popcorn hack and asked why it failed. Peers said the lesson felt long, with four examples and three homework methods covering overlapping ideas.


9. References

College Board. (2025). AP Computer Science A course and exam description (Effective fall 2025). Topic 4.8 ArrayList Methods, pp. 117-118

Runestone Academy. (n.d.). Topic 4.8: ArrayList and its methods. In CSAwesome2.