Section 12.3 Worked Example: Writing Arrays - Shifting Array Values
Subgoals for Evaluating Arrays.
-
Declaring an array variable
-
Determine data type to be stored
-
Determine name
-
Data type followed by [] followed by name
-
-
Instantiating an array object
-
Determine size (number of elements to be stored)
-
Assign to variable: keyword new, followed by datatype, followed by [size]. (All values initialized to default value for datatype.)
-
Assign to variable: {initializer list} where initializer list contains the values to be stored in the array
-
-
Accessing array element
-
Determine value of index for element to be accessed
-
arrayName[index]
returns value stored at that index -
index must be between 0 and
(arrayName.length-1)
, inclusive otherwise an IndexOutOfBounds exception occurs at runtime
-
-
Changing value of an array element
-
Determine value of index of element to be changed (remember rules for index values)
-
Determine the expression for RHS (remember the assignment subgoals for verifying data types)
-
Write assignment statement to update array element
-
-
Traversing an array
-
Decide if updating, accessing all in forward succession, or accessing some subset of array elements or accessing in a different order
-
If accessing only, write an enhanced for (for each) loop:
-
for (DataType varName : collectionName) - traverses collectionName from first element to last element storing a copy of each element from collectionName in varName for each iteration of the loop.
-
-
If updating or not accessing all elements in forward succession, write a for loop:
-
start loop control variable (which will also be index) at 0 to go forwards, (arrayName.length - 1) to go backwards
-
continuation test is loop control variable < arrayName.length for forwards, loop control variable >= 0 for backwards
-
update is loop control variable increments for forwards, decrements for backwards
-
-
inside loop access / update array element using loop control variable as index into array or varName has value of array element
-
-
Whole array actions
-
Passing an array as an argument
-
Determine that the an entire array must be passed as an argument to a method by consulting documentation
-
When calling a method, put variable name that represents the array as an argument in the method call. (Remember that when passing an array as an argument that changes made by the method to the array are persistent.)
-
-
Array Assignment
-
Determine that the reference to an array needs to be changed
-
The LHS of the assignment is the array reference needing to be changed
-
The RHS of the assignment is the new array reference
-
-
Subsection 12.3.1
Problem: Write the Java code that will declare an array of integers with the values 5, 10, 15, … 55 (multiples of 5). But then you remember you were supposed to start at 0, not 5 - so write the Java code that will shift all the values to the right by 1. In other words, the value 5 will shift from index 0 to index 1. And then you can put the value 0 at index 0.
Subsection 12.3.2 SG1: Declaring an array variable and SG2: Instantiating an array object
Because all multiples of 5 are integers, the datatype stored in the array should be int. We will name the variable
alpha
. The code to declare the array variable is:
int [] alpha;
In this case, we already know the values we want to store, so we’ll use an initializer list. However, initializer lists can only be used in the declaration, so the declaration becomes:
int [] alpha = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55};
Notice with an initializer list we don’t have to figure out the size, the system will automatically calculate the size for us. We also don’t need to write the loop to put the values in.
Subsection 12.3.3 SG5: Traversing an Array
For this problem, we will be updating and accessing all of the elements. This is a shift of the array elements “to the right”. If you look at a drawing of the array elements, the element currently at index 0 goes to index 1, the element at index 1 goes to index 2, and so on, which would mean that they all move towards the right in the drawing. In order to preserve the values, a shift “to the right” actually starts on the right (at index length-1) and moves to the left (towards index 0), so we are going to do a reverse traversal. So, we can set up the loop header:
for (int index = length -1; index > 0; index--)
Now, we need to decide what goes in the loop body. We need to move the elements to the right, so for each element, we need to take the value that is at
index-1
and move it to index
.
alpha[index] = alpha [index -1];
We have a problem at index zero, however. If we execute the above line of code when
index = 0
, we will get an ArrayIndexOutofBoundsException
when we try to compute index-1
. We need to stop our loop before that happens. After the loop, we need to add one more line of code to add the value zero into the first place into the array (i.e. at index 0).
To avoid this you may be inclined to attempt to copy moving forward through the Array rather than backwards. If you were to attempt this you would start the array at index 1 and end at index 9. The issue is after the first iteration index 1 will have 5 inside it like it should, but then following that all other indexes will copy the 5 into their slots, and the entire array will be filled with 5s. As a result we must iterate backwards.
Answer.
int [] alpha = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55};
for (int index = alpha.length - 1; index > 1; index--){
alpha[index] = alpha[index - 1];
}
alpha[0] = 0;
Subsection 12.3.4 Practice Pages
You have attempted 1 of 2 activities on this page.