4.03 Array Creation and Access
Create one-dimensional arrays, initialize values, access elements by index, and avoid invalid array positions.
1. Reference Guide
Key Topics
| Term | Definition | Example |
|---|---|---|
| Array | A fixed block of memory holding values of one type (the garage). | int[] listOne = new int[5]; |
| Element | One value in the array (a car in a spot). | listOne[0] = 5; |
| Index | Position of an element, starting at 0 (the spot number). | listOne[2] |
| Length | How many elements the array holds; public final, so it can’t change. |
listOne.length is 5 |
| Default value | What Java puts in every slot when you use new without values. |
new int[5] → [0, 0, 0, 0, 0] |
| Initializer list | Creates and fills the array in one step. | int[] listTwo = {1, 2, 3, 4, 5}; |
| Enhanced for loop | Visits each value without giving you the index. | for (int t : tickets) |
lengthispublic(anyone can read it) andfinal(locked at creation).- The last valid index is always
length - 1. System.out.println(arr)prints a memory reference like[I@28a6ceb, not the values.
Default Values
| Type | Default Value | Notes |
|---|---|---|
int |
0 |
|
double |
0.0 |
|
boolean |
false |
|
char |
'\u0000' |
Outside AP scope |
String and any object |
null |
No object exists until you create one |
Picking a Loop
- Need the index, or need to change values in the array → standard
forloop - Only need to read each value → enhanced
forloop - Stop condition isn’t just “reached the end” →
whileloop - Finding a min or max → any loop, starting your “best so far” at the first element
2. LxD Cycle Process
Empathize: Students print an array directly and get [I@28a6ceb, assume new Student[3] creates three students, and try to change values through an enhanced for loop and wonder why nothing happened. Research on novice programmers repeatedly finds weak mental models of memory and references, and array indexing errors among the most common difficulties (Qian & Lehman, 2017).
Define:
- POV: CSA students need a concrete picture of what
newactually builds, because the difference between “spots exist” and “cars are parked” explains default values,null, andNullPointerException. - Learning Goal: Students will create arrays with
newand with initializer lists, predict default values, fill arrays of objects, traverse with standard and enhanced for loops, and find a minimum or maximum.
Ideate:
- HMW Question: How might we get students to picture empty spots before they picture cars?
- HMW Question: How might we show that an enhanced for loop variable is a copy, not the real spot?
- Activity: Work a garage attendant shift in the popcorn hack, predict defaults and loop behavior in the MCQ, then find the longest-parked car in the homework.
Prototype:
- A reference guide, runnable Java examples built on the parking garage, a popcorn hack, an MCQ knowledge check, and a scaffolded grade rubric.
- Students revise loops after seeing
NullPointerException,ArrayIndexOutOfBoundsException, or unchanged values in the output. - Excellence means choosing the right loop for the job, explaining default values and
null, and improving the first attempt rather than only producing working code.
Test:
- Ask peers in peer review to complete the practice without additional explanation.
- Observe whether peers pick an indexed loop when they need to change values.
- Compare your lesson with another that is posted.
- Use the findings to revise any instruction or rubric criterion that did not guide students clearly.
- 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.3 Array Creation and Access. Summarized from the course and exam description (College Board, 2025, p. 109):
- Arrays are created with
newand a size, or with an initializer list. Arrays created withnewhold default values:0,0.0,false, ornull. - Elements are accessed with square brackets and an index from
0tolength - 1. Using an index outside that range throws anArrayIndexOutOfBoundsException. - Arrays can store primitive values or object references. Each object in an array of objects must be created separately.
The College Board says char is outside the scope of the AP Computer Science A course and exam. The default values table still lists it because it shows up in real code, but it will not be on the exam.
4. Lesson Plan
Learning Objective: Create arrays of primitives and objects, access and change their elements, and traverse them to find a minimum or maximum.
Success Criteria: You can predict default values, fill an array of objects without a NullPointerException, choose the right loop, and find the smallest or largest value.
Tech Talk (5 minutes)
Picture a parking garage under construction. The contractor is told to build exactly 5 spots. Once the concrete is poured, a sixth spot doesn’t exist unless the whole garage is torn down and rebuilt bigger. That is new int[5].
Pouring the concrete doesn’t park any cars. Java fills every spot with a default value, because “empty” isn’t something a computer can store. Parking a car is assignment: listOne[0] = 5;.
Code Runner Challenge
Run it, then change new int[5] to new int[7] and predict which lines change
View IPYNB Source
// CODE_RUNNER: Run it, then change new int[5] to new int[7] and predict which lines change
public class GarageBasics {
public static void main(String[] args) {
int[] listOne = new int[5]; // [0, 0, 0, 0, 0]
listOne[2] = 33; // [0, 0, 33, 0, 0]
listOne[3] = listOne[2] + 3; // [0, 0, 33, 36, 0]
try {
listOne[5] = 13; // no such spot
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error at listOne[5] = 13: that spot was never built!");
}
System.out.println(listOne); // the garage's address, not its contents
System.out.println(listOne[3]); // actually checks spot 3
}
}
GarageBasics.main(null);
5. Code Examples
A. Default values. The GarageDefaults class builds four empty garages and checks a spot in each
Three ways to get values in get mixed up a lot:
Code Runner Challenge
Run it, then predict what spot 1 holds in each garage after you add plates[1] = "7ABC123";
View IPYNB Source
int[] a = new int[5]; // creation: 5 spots, all 0
a[0] = 5; // assignment: park one car
int[] b = {1, 2, 3, 4, 5}; // initializer list: build the garage pre-loaded
Code Runner Challenge
Run it, then delete the line that parks a car in spot 1 and see what the loop prints
View IPYNB Source
// CODE_RUNNER: Run it, then predict what spot 1 holds in each garage after you add plates[1] = "7ABC123";
public class GarageDefaults {
public static void main(String[] args) {
int[] tickets = new int[3];
double[] fees = new double[3];
boolean[] paid = new boolean[3];
String[] plates = new String[3];
System.out.println("tickets[1]: " + tickets[1]);
System.out.println("fees[1]: " + fees[1]);
System.out.println("paid[1]: " + paid[1]);
System.out.println("plates[1]: " + plates[1]);
}
}
GarageDefaults.main(null);
Terminology:
Primitive arrays fill with a real value (0, 0.0, false). Object arrays fill with null, meaning “no object here yet.”
B. Arrays of objects. The GarageObjects class shows empty spots before any cars are parked
Code Runner Challenge
Run it, then change earliestTicket to find the highest ticket instead
View IPYNB Source
// CODE_RUNNER: Run it, then delete the line that parks a car in spot 1 and see what the loop prints
public class GarageObjects {
static class Car {
private String plate;
private int ticket;
public Car(String plate, int ticket) {
this.plate = plate;
this.ticket = ticket;
}
public String getPlate() {
return plate;
}
public String toString() {
return plate + " (ticket #" + ticket + ")";
}
}
public static void main(String[] args) {
Car[] level1 = new Car[3];
System.out.println("Spot 1 before parking: " + level1[1]);
try {
System.out.println(level1[1].getPlate());
} catch (NullPointerException e) {
System.out.println("NullPointerException: no car in spot 1 yet");
}
level1[0] = new Car("7ABC123", 102);
level1[1] = new Car("8XYZ789", 97);
level1[2] = new Car("5LMN456", 118);
int i = 0;
while (i < level1.length) {
System.out.println("Spot " + i + ": " + level1[i]);
i++;
}
}
}
GarageObjects.main(null);
Common object array mistake:
Code Runner Challenge
Replace the sample values with your own, then run it
View IPYNB Source
Car[] level1 = new Car[3]; // 3 empty spots, 0 cars
level1[0].getPlate(); // NullPointerException: nothing to ask
Terminology:
Printing null is fine. Calling a method on null is what throws the NullPointerException.
C. Enhanced for loops and min/max. The GarageWalkthrough class shows the copy rule and finds the longest-parked car
Code Runner Challenge
Fill in the blanks, then run it
View IPYNB Source
// CODE_RUNNER: Run it, then change earliestTicket to find the highest ticket instead
public class GarageWalkthrough {
public static int earliestTicket(int[] ticketNumbers) {
int lowest = ticketNumbers[0]; // assume the first car is the winner
for (int i = 1; i < ticketNumbers.length; i++) {
if (ticketNumbers[i] < lowest) {
lowest = ticketNumbers[i];
}
}
return lowest;
}
public static void main(String[] args) {
int[] tickets = {102, 118, 97, 145, 103};
for (int t : tickets) {
t = 0; // changes the copy only
}
System.out.println("After enhanced for, tickets[0] = " + tickets[0]);
for (int t : tickets) {
System.out.println("Ticket: " + t);
}
System.out.println("Earliest ticket: " + earliestTicket(tickets));
}
}
GarageWalkthrough.main(null);
Terminology:
The enhanced for loop variable is a copy of each value. Changing it is like scribbling on your own clipboard without repainting the number on the ground. Use an indexed loop to change the array.
6. Hacks & Practice Tasks
Prepare your submission IPYNB
- Create a new notebook in your portfolio homework area:
_notebooks/homework. - Add one raw cell at the top with the frontmatter (same fields as your other homework notebooks).
- Add code cells for the Popcorn Hack and the Homework Hack. Make sure every cell runs with visible output.
- Submit the link to your published page at the bottom of this page, and paste this in the description box:
Lesson: CSA 4.3 Array Creation and Access
MCQ 4.3: <paste your result line, such as 5/6 | answers: B,B,B,B,C,B>
Popcorn: int array with new, 3 spots filled, for loop prints every spot; String array printed with while loop (yes/no)
Homework: final garage name used (yes/no)
Homework: tickets = <values>, fees = <values>
Homework: earliest ticket = <value> (standard for), highest fee = <value> (enhanced for)
Homework: service charge added with an indexed loop, fee at spot 0 = <value>
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: you’re the new garage attendant on your first shift.
- Build a 5-spot
intgarage withnew - Park cars (ticket numbers) in 3 of the spots
- Walk every spot with a standard
forloop and call out its ticket number - Build a
Stringarray of 3 driver names, fill only 2 - Walk the drivers with a
whileloop and print each one
Replace the sample values with your own.
// CODE_RUNNER: Replace the sample values with your own, then run it
public class AttendantShift {
public static void main(String[] args) {
// Try writing your own code here first!
// Sample answer:
int[] spots = new int[5];
spots[0] = 102;
spots[2] = 118;
spots[4] = 97;
for (int i = 0; i < spots.length; i++) {
System.out.println("Spot " + i + ": " + spots[i]);
}
String[] drivers = new String[3];
drivers[0] = "Bob";
drivers[1] = "John";
int i = 0;
while (i < drivers.length) {
System.out.println("Driver " + i + ": " + drivers[i]);
i++;
}
}
}
AttendantShift.main(null);
Stretch: swap the String array for a class you built this year (nest it inside as a static class, like Car in Example B).
MCQ Check
6 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: Find the longest-parked car. Store the garage name in a final variable, five ticket numbers in an int array, and five fees in a double array. Find the earliest (smallest) ticket with a standard for loop, find the highest fee with an enhanced for loop, then add a $1.00 service charge to every fee using the loop that can actually change the array.
Solution Skeleton:
// CODE_RUNNER: Fill in the blanks, then run it
public class LongestParked {
public static void main(String[] args) {
final String GARAGE_NAME = "Your Garage Name";
// 1. Ticket numbers and fees for 5 parked cars
int[] tickets = {0, 0, 0, 0, 0};
double[] fees = {0.0, 0.0, 0.0, 0.0, 0.0};
// 2. Earliest (smallest) ticket with a standard for loop
int earliest = tickets[0];
// 3. Highest fee with an enhanced for loop
double highestFee = fees[0];
// 4. Add a 1.00 service charge to every fee (which loop can change the array?)
System.out.println(GARAGE_NAME);
System.out.println("Earliest ticket: " + earliest);
System.out.println("Highest fee: " + highestFee);
System.out.println("Fee at spot 0 after service charge: " + fees[0]);
}
}
LongestParked.main(null);
Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Popcorn | 0.2 | int array made with new, 3 spots filled, every spot printed with a for loop, String array printed with a while loop, and the cell runs. |
| MCQ | 0.2 | 5 or 6 correct. 0.15 for 3 or 4, 0.1 if every question was answered. |
| Homework: arrays | 0.15 | final garage name, five int tickets, and five double fees. |
| Homework: minimum | 0.15 | Earliest ticket found with a standard for loop and printed. |
| Homework: maximum | 0.15 | Highest fee found with an enhanced for loop and printed. |
| Homework: modify | 0.15 | Service charge added to every fee with an indexed loop, and the change shows in the output. |
| Total | 1.0 |
Quick Validation Checklist
- Each cell ends with
ClassName.main(null);and shows output. - MCQ score in the notes.
finalon the garage name.- Minimum with a standard loop, maximum with an enhanced loop.
- Fees changed with an indexed loop, not an enhanced one.
- No array printed directly with
System.out.println(arr).
7. Lesson Revisions
Revision Made: Lesson authors: what you changed because of it.
8. Feedback Evidence
Feedback Received: Lesson authors: what your peers said in the practice run.
9. References
College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025].
Oracle. (n.d.). Arrays. The Java Tutorials.
Qian, Y., & Lehman, J. (2017). Students’ misconceptions and other difficulties in introductory programming: A literature review. ACM Transactions on Computing Education, 18(1), Article 1.