Skip to main content

Section 9.8 Worked Example: Arrays - Initializer List and Reverse Traverse

Subsection 9.8.1 Subgoals for Evaluating Arrays

  1. Set up array from 0 to size-1

  2. Evaluate data type of statements against array

  3. Trace statements, updating slots as you go

    1. 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]+ " ");

Subsection 9.8.2 SG1: Set up array from 0 to size-1

int [] alpha = {15, 24, 7, 6, -4, 0, 13};
Figure 9.8.1.
  • 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.

Subsection 9.8.3 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.

Subsection 9.8.4 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:

Figure 9.8.2.

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

Subsection 9.8.5 Practice Pages

You have attempted of activities on this page.