Unit 7: ArrayList

AP Computer Science A: 49 practice questions with detailed explanations.

Unit Study Guide

Executive Summary

ArrayList grows and shrinks at runtime, storing objects (Integer, not int). It is the dynamic alternative to arrays.

Creation and generics

ArrayList<Integer> list = new ArrayList<Integer>() stores Integer objects thanks to autoboxing. size() reports how many elements exist; indexes again run 0 to size() - 1.

Core methods

add(value) appends. add(index, value) inserts and shifts the rest right. get(index) reads. set(index, value) replaces and returns the old value. remove(index) deletes, shifts the rest left, and returns the removed element. size() is the count.

Traversals

Use an indexed loop to read AND write. Enhanced for works for reading only. Removing while traversing forward skips elements — go backward or adjust the index.

Searching and algorithms

Linear search with get(index). Find min/max with a running comparison. Sum with an accumulator. Reversal swaps the first half with mirrored positions.

Exam traps

remove(int) removes by INDEX on an Integer list, not by value. get(size()) throws IndexOutOfBoundsException. add at the front shifts everything, which is why appending is cheaper than inserting.

Top 5 Concepts to Master

  1. 1Choose between arrays and ArrayLists.
  2. 2Trace add/remove index shifts.
  3. 3Traverse and search with get and size.
  4. 4Handle removal-during-traversal correctly.

Key Terms & Definitions

Practice with Flashcards
ArrayList

Resizable list that stores objects.

Autoboxing

Automatic int-to-Integer conversion on add.

size()

Method returning the element count.

add

Appends or inserts an element.

set

Replaces an element, returns the old value.

remove

Deletes by index, shifts elements left.

IndexOutOfBoundsException

Thrown when get/set/remove use an invalid index.

Common Misconceptions: Exam Traps

ArrayList can store primitives directly.

Correct: It stores wrapper objects like Integer.

list.remove(0) removes the value 0.

Correct: With an int argument it removes the element at index 0.

Enhanced for can modify elements.

Correct: The loop variable is a copy.

size() and length work the same way.

Correct: size() is a method; arrays use the length field.

Question Bank Breakdown

By difficulty

easy 18medium 29hard 2

By topic

ArrayList Methods 36Traversing ArrayLists 13

All Questions in this Unit