Skip to main content

Section 9.20 Worked Example: Arrays - Shift

Subsection 9.20.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: 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;

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

Figure 9.20.1.

Subsection 9.20.3 SG2: Evaluate data type of statements against array

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

Subsection 9.20.4 SG3: Trace statements, updating slots as you go

Let us trace with a sample array.
Figure 9.20.2.
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.
Figure 9.20.3.
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:
Figure 9.20.4.
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.”

Subsection 9.20.5 Practice Pages

You have attempted of activities on this page.