Skip to main content

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

Subgoals for Evaluating Arrays.

  1. Declaring and initialization of array
    1. Set up a one dimensional table (i.e., one row) with 0 to (size - 1) elements
    2. Upon instantiation of an array object, all elements contain default value for datatype stored in array OR values from the initializer list
  2. Determine access or change of element, or action on entire array object, and update slots as needed (remembering assignment subgoals)
  3. Accessing array element
    1. Evaluate expression within [] which will be the index for element to be accessed
    2. arrayName[index] returns value stored at that index
    3. index must be between 0 and arrayName.length - 1, inclusive otherwise IndexOutOfBounds exception occurs
  4. Changing value of an array element
    1. Evaluate expression within [] which will be the index for element to be accessed
    2. arrayName[index] will now contain the value on the RHS of assignment statement
    3. (remember the assignment subgoals for verifying data types and evaluating expressions)
    4. (remember rules for index values)
  5. Whole array actions
    1. Pass as argument - a copy of the reference to the instantiated array is passed to the method. This means that any changes made to the array elements inside the method are persistent. The one exception to this is if you assign the argument to reference a different array in memory.
    2. Assignment - changes the reference to point to the array on the RHS of the assignment operator.

Subsection 11.6.1

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 11.6.2 SG1: Declaring and initialization of array

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.
  • Because an initializer list is used, instead of the default values based on data type, the values from the initializer list are placed directly into the array.
Figure 11.6.1.

Subsection 11.6.3 SG2: Determine access or action

Within the loop, there is both access (SG3) and changing of elements (SG4). There are no actions on the entire array, which would be indicated with just an array name and no [ ].

Subsection 11.6.4 Evaluating code

System.out.println(alpha.length);
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,
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 through 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.
All index values are within bounds and only integer values are being copied into the array.
The resulting array is:
Figure 11.6.2.
The second for-loop
for (int i = alpha.length-1; i >= 0; i--)
    System.out.print(alpha[i]+ " ");
also iterates in reverse with i going from value 6 to value 0. This code will print the values in the array in reverse order.
14 1 -3 7 8 25 16 is printed.
Answer.
7
14 1 -3 7 8 25 16

Subsection 11.6.5 Practice Pages

You have attempted of activities on this page.