Section 12.1 Worked Example: Writing Arrays - Storing Multiplication Table
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.1.1
Problem: Write the Java code to store the multiplication table for the value 13. The first element should be 0 (13 * 0), the second element should be 13 (13 * 1), the third element should be 26 (13 * 2), etc. There should be a total of 51 elements in the array.
Subsection 12.1.2 SG1: Declaring an array variable
Because all multiples of 13 are integers, the datatype stored in the array should be int. We will name the variable
mult13
. The code to declare the array variable is:
int [] mult13;
Subsection 12.1.3 SG2: Instantiating an array object
We want to store 51 elements in the array
mult13
:
mult13 = new int[51];
Subsection 12.1.4 SG5: Traversing an array
Before we actually access or update the array (SG3 and SG4), it is important for us to realize that we will need to update all the elements of the array. (As currently declared and instantiated the array mult13 would contain 51 0s - the default value for integers.) So we will first discuss the traversal of the array.
We will be traversing the entire array, updating all the elements. Because we will be updating the values within the array, we need to use a for loop (subgoal 5C). We can go forwards in our loop, from index 0 to index length - 1, incrementing by 1:
for (int index = 0; index < mult13.length; index++)
Subsection 12.1.5 SG4: Changing the value of an array element
Inside the loop, we want to change the current value stored in that array element (at index). The expression for the RHS, should be the literal value 13 multiplied by the loop control variable (index). So the statement inside the loop should be:
mult13[index] = index * 13;
Subsection 12.1.6
What is the final completed code? How would you know that you had stored the correct values in the array?
Answer.
int [] mult13;
mult13 = new int[51];
for (int index = 0; index < mult13.length; index++) {
mult13[index] = index * 13;
}
To verify we have the correct values, we can print out the values of the array:
int i = 0;
for (int x : mult13) {
System.out.println("13 * " + i + " is " + x);
i += 1;
}
Notice that in this code we are only accessing values in the array, so we can use an enhanced for loop (or a for-each loop). However, because we want to print the number that 13 is multiplied by, we need another variable to keep track of that (which is identical to the index). The for-each loop does not allow access to an index value.
Subsection 12.1.7 Practice Pages
You have attempted of activities on this page.