Skip to main content
← AP CSA Study Guide

Unit 5 · Heavy

Arrays

Fixed-size indexed storage, and the first place two variable names can refer to the same data. Aliasing is what makes array questions harder than they look.

What a strong answer looks like

A strong Unit 5 answer says which indexes are visited, whether the array is read or mutated, and whether any other name refers to the same array.

Topics in this unit

1

Array Basics

Practice this topic →

Know

An array has a fixed length set at creation and is accessed by index from zero. Its elements start at a default value rather than being empty.

Apply

State the length and the valid index range before writing any loop over it.

Watch out

Confusing length, which is a field on arrays, with length(), which is a method on Strings.

Study move

Declare arrays of three different types and state each default element value.

2

Array Traversal

Practice this topic →

Know

An indexed loop can read and write elements; an enhanced for loop reads a copy of each element and cannot replace it.

Apply

Choose the indexed form whenever the array must be modified.

Watch out

Assigning to the enhanced for variable and expecting the array to change. It does not.

Study move

Attempt to double every element with an enhanced for loop, then explain why it fails.

3

Array Mutation

Practice this topic →

Know

Assigning to an index changes the stored value. Because arrays are objects, passing one to a method passes a reference, so the method can change the caller’s array.

Apply

Trace array contents after each assignment, and note when two names alias one array.

Watch out

Assuming a method received a copy. It received a reference, and its changes persist.

Study move

Write a method that modifies its array parameter and confirm the change is visible to the caller.

4

Array Search

Practice this topic →

Know

Linear search inspects elements in order and works on unsorted data. It must handle the not-found case explicitly.

Apply

Return a sentinel such as negative one when nothing matches, and stop as soon as a match is found.

Watch out

Returning from inside the loop on a mismatch, which reports failure after checking only the first element.

Study move

Write a linear search returning the index of the first match, then test it on an array with no match.

Emphasized in this unit

Connections and techniques that receive extra attention in this unit.

  • Distinguishing reading from mutating
  • Recognising aliasing when an array is passed to a method
  • Handling the not-found case in every search

Varies by course

Related topics some schools attach to this unit and others leave out. Covered on request rather than assumed.

  • Array resizing. Copy-and-grow patterns are shown in some classes; ArrayList covers this on the exam.
  • Arrays utility methods. Occasionally used in class, but outside the tested subset.

Mastery checklist

  • State the valid index range of an array of length n.
  • Choose between indexed and enhanced for based on whether mutation is needed.
  • Explain why a method can change the caller’s array.
  • Write a linear search that handles no match.

Check yourself

  • Why does assigning to an enhanced for variable not change the array?
  • What does it mean for two variables to alias one array?
  • Where should a search return, and where should it not?

Modeling drill

Write a method that doubles every even value in an array and leaves odd values alone, then state what the caller sees afterwards.

IndexlengthEnhanced forAliasingReferenceLinear searchSentinel