Teacher Guide: 1D Arrays
Student page: 1D Arrays IB Syllabus: B2.2.2 Estimated periods: 2 (90 min each) Prerequisites: Selection, Iteration
Contents
Lesson Plans
Period 1: Declaration, Traversal, Basic Operations
| Phase | Time | Activity | Student Page Section |
|---|---|---|---|
| Warm-up | 10 min | Mailbox analogy — draw numbered boxes 0-4, “why start at 0?” | — |
| Teach | 25 min | Declaration (two forms), traversal pattern for (int i = 0; i < arr.length; i++), print all elements | Key Concepts |
| Practice | 40 min | Core #1-3 (Input and Print, Find Minimum, Count Above Threshold) | Practice Exercises |
| Wrap-up | 15 min | Quick Code Check MCQs | Quick Code Check |
Teaching notes:
- Drill the canonical traversal pattern until automatic:
for (int i = 0; i < arr.length; i++) - Derive common operations, don’t give them: ask “How would you find the largest? describe in words first”
- Seed min/max with first element, NOT with 0 or Integer.MAX_VALUE at this stage
Additional notes:
- Use a running example (e.g., monthly phone bills) throughout to give context to each operation
- Common trap: initializing min to 0 instead of the first array element — always seed with
arr[0]
Period 2: Reference Trap, Copy, Resize, Advanced Operations
| Phase | Time | Activity | Student Page Section |
|---|---|---|---|
| Warm-up | 10 min | Reference trap discovery: int[] b = a; b[0] = 99; — predict a[0] | — |
| Teach | 20 min | Reference vs value copy, element-by-element copy, resize pattern | Worked Examples |
| Practice | 45 min | Core #4, Extension #5-6, Challenge #7 | Practice Exercises |
| Wrap-up | 15 min | Trace exercise + connect to ArrayList preview | Trace Exercise |
Additional activities:
- GradeArray mini-project (input validation, average, highest, lowest, repeat/quit menu)
- DuplicateChecker: compare O(n²) any-duplicate vs O(n) all-same approaches
- 4-step array resizing pattern (create larger, copy, assign, use)
- Real-world exercises: sensor readings with tolerance (
Math.abs), event registrations, flight seating chart
Differentiation
Supporting weaker students:
| Strategy | When to use | Example |
|---|---|---|
| Array diagram | Can’t visualize indices | Draw boxes with index numbers above: [0]=8, [1]=3, [2]=15… |
| Traversal template | Can’t write the for loop | Provide: for (int i = 0; i < arr.length; i++) { /* use arr[i] */ } |
| One operation at a time | Overwhelmed by Core #2 | First just print all elements, then find minimum separately |
| Pair programming | During Core exercises | One student traces on paper, other codes |
Extending stronger students:
| Strategy | When to use | Example |
|---|---|---|
| GradeArray mini-project | Finishes Core early | Input validation + average + highest + lowest + repeat menu |
| DuplicateChecker | After Extension #6 | Compare O(n²) any-duplicate vs O(n) all-same approaches |
| Real-world exercises | After Core | Sensor readings with Math.abs tolerance, flight seating chart |
| Challenge #7 | Extended learners | Resize and insert — connects to ArrayList internal mechanism |
| Frequency counter | After Extension | Count occurrences of each unique value in the array |
Answer Keys
Core 1: Input and Print
public class InputAndPrint {
public static void main(String[] args) {
int[] arr = {8, 3, 15, 7, 12};
for (int i = 0; i < arr.length; i++) {
System.out.println("[" + i + "] = " + arr[i]);
}
}
}
Expected output:
[0] = 8
[1] = 3
[2] = 15
[3] = 7
[4] = 12
Common mistakes: Using <= arr.length (ArrayIndexOutOfBoundsException); printing without index
Marking notes: Must use loop (not 5 separate println statements). Must show index with each element.
Core 2: Find Minimum
public class FindMinimum {
public static void main(String[] args) {
int[] temps = {22, 18, 25, 15, 20};
int min = temps[0];
int minIndex = 0;
for (int i = 1; i < temps.length; i++) {
if (temps[i] < min) {
min = temps[i];
minIndex = i;
}
}
System.out.println("Minimum: " + min + " at index " + minIndex);
}
}
Expected output: Minimum: 15 at index 3
Common mistakes: Initializing min to 0 instead of temps[0]; starting loop at 0 instead of 1 (wastes a comparison but still works); using <= in loop condition
Marking notes: Must seed with first element. Starting loop at i=0 is acceptable (redundant but not wrong). Key trap: don’t initialize min to 0.
Core 3: Count Above Threshold
public class CountAbove {
public static void main(String[] args) {
int[] scores = {72, 85, 60, 91, 45, 78};
int count = 0;
for (int i = 0; i < scores.length; i++) {
if (scores[i] >= 70) {
count++;
}
}
System.out.println(count + " scores are 70 or above");
}
}
Expected output: 4 scores are 70 or above
Common mistakes: Using > 70 instead of >= 70 (off by one — 70 should be included); forgetting to initialize count to 0
Marking notes: The boundary condition (>= vs >) is the key check. Count should be 4 (72, 85, 91, 78).
Core 4: Sum of Even Elements
public class SumEven {
public static void main(String[] args) {
int[] data = {3, 8, 15, 12, 7, 20, 5};
int sum = 0;
for (int i = 0; i < data.length; i++) {
if (data[i] % 2 == 0) {
sum += data[i];
}
}
System.out.println("Sum of even elements: " + sum);
}
}
Expected output (using the sample array): Sum of even elements: 40 (8 + 12 + 20 = 40)
Common mistakes: Checking if the INDEX is even instead of the VALUE; using data[i] / 2 == 0 instead of data[i] % 2 == 0
Marking notes: Accept any reasonable test array. Key: must check value (data[i]) not index (i).
Extension 5: Reverse Copy
public class ReverseCopy {
public static void main(String[] args) {
int[] original = {1, 2, 3, 4, 5};
int[] reversed = new int[original.length];
for (int i = 0; i < original.length; i++) {
reversed[original.length - 1 - i] = original[i];
}
// Print original
System.out.print("Original: ");
for (int i = 0; i < original.length; i++) {
System.out.print(original[i] + " ");
}
System.out.println();
// Print reversed
System.out.print("Reversed: ");
for (int i = 0; i < reversed.length; i++) {
System.out.print(reversed[i] + " ");
}
System.out.println();
}
}
Expected output:
Original: 1 2 3 4 5
Reversed: 5 4 3 2 1
Common mistakes: Modifying the original array; wrong index formula for reverse mapping; off-by-one in length - 1 - i
Marking notes: Must NOT modify original. Accept alternative: fill reversed from end using reversed[reversed.length - 1 - i] = original[i].
Extension 6: Remove Duplicates
public class RemoveDuplicates {
public static void main(String[] args) {
int[] data = {3, 7, 3, 2, 7, 5, 2};
int[] temp = new int[data.length];
int uniqueCount = 0;
for (int i = 0; i < data.length; i++) {
boolean isDuplicate = false;
for (int j = 0; j < uniqueCount; j++) {
if (data[i] == temp[j]) {
isDuplicate = true;
}
}
if (!isDuplicate) {
temp[uniqueCount] = data[i];
uniqueCount++;
}
}
// Print result
System.out.print("Unique: ");
for (int i = 0; i < uniqueCount; i++) {
System.out.print(temp[i] + " ");
}
System.out.println();
}
}
Expected output: Unique: 3 7 2 5
Common mistakes: Comparing each element only to its immediate neighbor (that only works if sorted); not maintaining insertion order
Marking notes: O(n²) nested loop is the expected approach at this level. Must preserve first-occurrence order. Accept break after finding duplicate.
Challenge 7: Resize and Insert
public class ResizeAndInsert {
public static void main(String[] args) {
int[] data = {10, 20, 30, 40, 50};
int insertValue = 25;
int insertIndex = 2;
// Create new array one element larger
int[] newData = new int[data.length + 1];
// Copy elements before insert point
for (int i = 0; i < insertIndex; i++) {
newData[i] = data[i];
}
// Insert the new value
newData[insertIndex] = insertValue;
// Copy remaining elements, shifted right by 1
for (int i = insertIndex; i < data.length; i++) {
newData[i + 1] = data[i];
}
// Print result
System.out.print("Result: ");
for (int i = 0; i < newData.length; i++) {
System.out.print(newData[i] + " ");
}
System.out.println();
}
}
Expected output: Result: 10 20 25 30 40 50
Common mistakes: Trying to insert into the original array (arrays can’t be resized); wrong shift direction (must shift RIGHT, not left); off-by-one in the second copy loop
Marking notes: Must create a new array. The two-loop approach (before insert, after insert) is clearest. Accept single-loop approaches with conditional. Connect to ArrayList: “This is what ArrayList.add(index, value) does internally.”
Integration Notes
| Item | In Class | Homework | Link |
|---|---|---|---|
| Worked Examples | Walk through together | — | Worked Examples |
| Quick Code Check | End of Period 1 | — | Quick Code Check |
| Core #1-3 | Period 1 practice | — | Practice Exercises |
| Core #4, Extension #5-6 | Period 2 practice | Complete for homework | Practice Exercises |
| Challenge #7 | — | Optional extension | Practice Exercises |
| Trace Exercise | Period 2 wrap-up | — | Trace Exercise |
| GitHub Classroom | — | Due end of week | GitHub Classroom |
Additional Resources
- Running example (e.g., monthly phone bills) for live coding reference
- GradeArray mini-project — extended homework for stronger students
- Array operation reference card: traversal, find max/min, sum, count, copy patterns