9.3. Worked Example: Arrays - Initializer List and Reverse Traverse¶
Subgoals for Evaluating Arrays
Set up array from 0 to size-1
Evaluate data type of statements against array
Trace statements, updating slots as you go
Remember assignment subgoals
You can watch this video or read through the content below it.
Problem: Evaluate these statements and determine their output
int [] alpha = {15, 24, 7, 6, -4, 0, 13};
System.out.println(alpha.length);
for (int i = alpha.length-1; i >= 0; i--)
alpha[i] = alpha[i] + 1;
for (int i = alpha.length-1; i >= 0; i--)
System.out.print(alpha[i]+ " ");
SG1: Set up array from 0 to size-1
int [] alpha = {15, 24, 7, 6, -4, 0, 13};

alpha is declared as an array of ints
This statement allocates 7 slots for integers because there are 7 literal values given in the initialization list.
SG2: Evaluate data type of statements against array
The statement System.out.println(alpha.length);
will print an output String. Then,
for (int i = alpha.length-1; i >= 0; i--)
alpha[i] = alpha[i] + 1;
This loop has index i go from 6 to 0 by decrements of 1.
All of the array indexes are within the bounds of the array (6 – 0) and the value of 1 is added to the current value at position i in the array.
All values being assigned are valid integers and can be stored in an integer array.
The final for-loop will also print output strings.
SG3: Trace statements, updating slots as you go
The statement System.out.println(alpha.length);
will print the value 7 and then a newline return, so that the next output will begin on a new line. Then, the first for-loop will update the array values (without printing them quite yet!)
for (int i = alpha.length-1; i >= 0; i--)
alpha[i] = alpha[i] + 1;
The resulting array is:

The final for-loop will print: 14 1 -3 7 8 25 16
Practice Pages