📦 Arrays

Arrays: In Java an array is a fixed-size block of numbered cells allocated side by side in memory, with indexing starting at 0 — like a numbered parking lot whose capacity is pou

In Java an array is a fixed-size block of numbered cells allocated side by side in memory, with indexing starting at 0 — like a numbered parking lot whose capacity is poured in concrete up front: each spot (index) holds one car (value), running from [0] to [4], but once the concrete is set you cannot add new spots. But if Java already has the flexibly growing ArrayList, why still learn fixed-size arrays? Because an array is contiguous in memory so access is the fastest, and ArrayList actually uses an array inside; without knowing the foundation you cannot solve "index out of bounds" errors. This is close to a C array, unlike Python's dynamic list; to grow you must copy into a new array with `Arrays.copyOf`. For a QA engineer the off-by-one risk is critical: writing `<= length` in a loop that fills a fixed-size array throws `ArrayIndexOutOfBoundsException`, and such boundary errors are the source of typical "breaks under certain conditions" flaky bugs that appear when the test data grows.

Array Parking Lot Model

nums[2] is the third box. Java array indices start at 0; nums[5] does not exist in this array.

One-Dimensional Array

Micro Lab: Code practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Reverse an Array — Two Pointers

Array `a` starts with three elements. Goal: reverse it in place — without creating a new array.

`i` starts at the front (0), `j` at the back (2). These two pointers move toward each other until they meet in the middle.

Condition `i < j` → `0 < 2` is true → enter the body. The two pointers have not met yet.

`t = a[i]` → the value of a[0] (10) is saved in a temp variable — so it is not lost before we overwrite it.

`a[i] = a[j]` → a[0] is now the value of a[2] (30). The array is `[30, 20, 30]`; 10 is still safe in `t`.

`a[j] = t` → a[2] is now the saved value (10). Swap complete: `[30, 20, 10]`. Without the temp variable we would have lost 10.

`i++` and `j--` → the pointers step inward: i=1, j=1. They are now at the same spot.