Section 12.5 Worked Example: Write Arrays - Reverse
Subgoals for Writing 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 only accessing elements, use an enhanced for (for-each) loop:
-
for (DataType varName : collectionName)
- traverses collectionName by iterating from first element to last element storing a copy of each element from collectionName in varName.
-
-
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 use Subgoals 3 or 4 to access or change values as appropriate
-
-
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, pass a reference to an array (usually variable name) 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.)
-
-
Subsection 12.5.1
You can watch this video or read through the content below it.
Problem: Write the code to βreverseβ the elements of an array.
There are several different approaches you might take. We will examine two options here.
Subsection 12.5.2 Solution 1 β copy to new array with reverse traverse
We will begin with a sample array called
original
, and another array of the same size called copy
.
int [] original = {2, 4, 6, 8, 10};
int [] copy = new int [original.length];
Then we loop in reverse for the original array, copying the elements in order and placing them starting at the beginning of the new array. The variable
place
helps us with traversing the new array in the opposite direction of our reversed traversal of the original.
int place = 0;
for (int i = original.length - 1; i >= 0; i++)
copy[place++] = original[i];
Subsection 12.5.3 Solution 2 β same array with swaps
In this solution, we do not need the whole extra array in memory. The loop only traverses half of the array, swapping the value at
i
with the value at the mirrored/reflected position array.length - i - 1
.
for (int i=0; i<array.length/2; i++) {
int temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
Subsection 12.5.4 Practice Pages
You have attempted of activities on this page.