9.10. Worked Example: Arrays - Shift

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: Assume that the integer array alpha has been properly declared, has a length greater than or equal to 1, and is full of data values. Evaluate these statements and determine the values in alpha. If any error occurs, give the reason.

int start = alpha[0];
for (int i = 0; i < alpha.length - 1; i++) {
   alpha[i] = alpha[i+1])
}
alpha[length - 1] = start;

SG1: Set up array from 0 to size-1

Instantiated Array

SG2: Evaluate data type of statements against array

All indexes into the array are valid, and all assignments are valid.

SG3: Trace statements, updating slots as you go

Let us trace with a sample array.

Sample Array

For this example, the first statement, int start = alpha[0]; gives start a value of 12. We need to save this value to use at the end, so we do not lose it during the shift.

Then a for-loop is used to traverse the array and copy the value at index i+1 into position at index i. The chart below shows the assignment for every iteration of the loop, as well as the state of the array after the loop is complete.

Trace loop as it shifts values

The final statement, alpha[length - 1] = start;, places the saved start value into position at the end of the array. The final array is shown below:

Resulting Array

The more general answer to the original question is: “alpha contains the same values, but their positions have shifted down/left by 1 index each, with the value at index 0 now at index array.length - 1.”

Practice Pages

You have attempted of activities on this page