Chapter 10 · Simulation

Array Lab

← Chapter 10
int[] arr
    Variables

    Array Lab

    Purpose: see that an array consists of cells that sit side by side in memory, that every element is reached through its index (from 0 to length − 1), and how the classic array algorithms work step by step.

    How to use it

    1. Type the elements separated by commas and press Apply, or create a Random array.
    2. Manual read/write: enter an index and press Read or Write. An invalid index (negative, or length and above) throws ArrayIndexOutOfBoundsException; the red dashed cell shows the position that does not exist.
    3. In the other exercises move on with Step ▶: the cell being read has a blue ring, the cell being written is orange, the cell that was found is green; the pointers (i, left, right) are above the cells. → / ← work on the keyboard too.
    4. Try it yourself: predict the comparison results and the values that will be written.

    Reminders

    • Valid indices are 0 … arr.length − 1; the loop condition must be i < arr.length, while <= runs one iteration too many.
    • In arr[i] = arr[arr[i]] the inner index arr[i] is read first, then the element at that position; cells changed in earlier iterations are read with their new values.
    • The error message format is that of Java 11 and later (Index 4 out of bounds for length 4); Java 8 prints only the index.

    Related slides: Chapter 10 · Arrays (array and index, out-of-bounds error, sum and average, smallest and largest, reversal, linear search).