Creation, Indexing

A 1D array is a fixed-length container of elements of a single type.

What to practice

  • Create, initialize, and traverse 1D arrays of primitives and object references.
  • Distinguish when an enhanced for-each loop is valid vs. when an indexed for loop is required.
  • Implement standard 1D array algorithms: min/max, sum/average, mode, element shifting/rotation, and adjacent-element checks.

Current CED topics: 4.3, 4.4, 4.5

Current learning objectives: 4.3.A, 4.4.A, 4.5.A

1. Core Concept Instruction

A 1D array is a fixed-length container of elements of a single type, indexed from 0 to arr.length - 1 (note that arr.length has no parentheses, unlike str.length()). When constructed via new int[N], numeric elements default to 0 (or 0.0 for double), booleans default to false, and object references default to null. An enhanced for-each loop for (int val : arr) assigns a copy of each element to the local loop variable val in order from index 0 to arr.length - 1. Because val is a local copy, assigning val = val * 2 inside a for-each loop NEVER modifies the underlying array! Use a standard indexed for loop whenever you need to modify array elements, access indices, compare adjacent elements (arr[i] and arr[i+1]), or traverse backward/partially.

2. Worked Examples

One-Position Left Rotation of an Array

Trace the contents of int[] nums = {10, 20, 30, 40, 50}; after executing:
int first = nums[0];
for (int i = 0; i < nums.length - 1; i++) {
  nums[i] = nums[i + 1];
}
nums[nums.length - 1] = first;
  1. Step 1 (Save Element at Index 0): first stores 10 before index 0 is overwritten.
  2. Step 2 (Trace Loop Iterations i = 0..3): i=0: nums[0] = nums[1] (20); i=1: nums[1] = nums[2] (30); i=2: nums[2] = nums[3] (40); i=3: nums[3] = nums[4] (50). Notice the loop bound i < nums.length - 1 stops at i = 3 so nums[i + 1] never exceeds index 4.
  3. Step 3 (Place Saved Element at End): nums[4] = first (10), completing a left rotation: {20, 30, 40, 50, 10}.
  4. Step 4 (Contrast with Right Shift): Note that if you instead wrote nums[i + 1] = nums[i] in a forward loop i = 0..3, every element would be overwritten with nums[0] (10) — a right shift requires looping BACKWARD from i = nums.length - 1 down to 1!

Result:

One-Position Right Rotation of an Array

Trace the array state after executing a single right-shift pass on int[] data = {4, 8, 12, 16}:
int last = data[data.length - 1];
for (int i = data.length - 1; i > 0; i--) {
  data[i] = data[i - 1];
}
data[0] = last;
  1. Step 1 (Save rightmost element): last stores data[3] = 16; backward loop shifts data[2]->data[3] (12), data[1]->data[2] (8), data[0]->data[1] (4).
  2. Step 2 (Place saved element at index 0): data[0] = 16 completes the circular right rotation without overwriting unread elements.

Result: data = {16, 4, 8, 12}

Use this chapter companion

Review the lesson, attempt the original practice, then check the explanation. Your printed study guide remains complete without the website.

Independence

Skill by Skill Prep is independent. SAT® and AP® are trademarks registered by the College Board, which is not affiliated with, and does not endorse, this website or book. PSAT/NMSQT® is a registered trademark of the College Board and the National Merit Scholarship Corporation, which are not affiliated with, and do not endorse, this site. Bluebook™ is a trademark of the College Board. ACT® is a trademark of ACT Education Corp., which is not affiliated with and does not endorse this website or book. Desmos is a trademark of Desmos Studio PBC. Practice results describe these questions, not official scores or admission predictions.

Last updated .