Section 11.3 Worked Example: Arrays - Traverse
Subgoals for Evaluating Arrays.
-
Declaring and initialization of array
- Set up a one dimensional table (i.e., one row) with 0 to (size - 1) elements
- Upon instantiation of an array object, all elements contain default value for datatype stored in array OR values from the initializer list
- Determine access or change of element, or action on entire array object, and update slots as needed (remembering assignment subgoals)
-
Accessing array element
- Evaluate expression within [] which will be the index for element to be accessed
arrayName[index]
returns value stored at that index- index must be between 0 and
arrayName.length
- 1, inclusive otherwiseIndexOutOfBounds
exception occurs
-
Changing value of an array element
- Evaluate expression within [] which will be the index for element to be accessed
arrayName[index]
will now contain the value on the RHS of assignment statement- (remember the assignment subgoals for verifying data types and evaluating expressions)
- (remember rules for index values)
-
Whole array actions
- Pass as argument - a copy of the reference to the instantiated array is passed to the method. This means that any changes made to the array elements inside the method are persistent. The one exception to this is if you assign the argument to reference a different array in memory.
- Assignment - changes the reference to point to the array on the RHS of the assignment operator.
Subsection 11.3.1
Problem:
Given the initialized array:
int [] alpha;
Evaluate these statements and determine the value of all variables. If any error occurs, give the reason.
alpha = new int[10];
for (int i = 0; i < 10; i++)
alpha[i] = i * 10;
Subsection 11.3.2 SG1: Declaring and initialization of array
alpha = new int[10];
- alpha is declared as an array of int
- This statement allocates 10 slots for integers (first line is indexes, second line is values)
- Declared size of array is 10, so last valid index value is 9.
Subsection 11.3.3 SG2: Determine access or action
In this example, an array is declared and instantiated to hold 10 new integers. Within the loop, we will be changing values within the array, so we look to SG4.
Subsection 11.3.4 SG4: Change array element
There is a loop within the example. Looking at the loop header:
for (int i = 0; i < 10; i++)
The loop control variable (
i
) will iterate from the value 0 to the value 9. So there will be 10 total iterations. For each iteration, the statement:alpha[i] = i * 10;
is executed. Notice that the loop control variable (
i
) is used as both the index for the element within the array to be changed and for its value on the RHS of the assignment statement.- When
i
is 0 (the first iteration of the loop), 0 is copied intoalpha[0]
. - When
i
is 1 (the second iteration of the loop), 10 (1 * 10) is copied intoalpha[1]
. - This pattern continues until
i
is 9, where 90 (9 * 10) is copied intoalpha[9]
.
Note that we have to verify that the index values are all within the array bounds, and that the values being assigned are legal in terms of data type.
The resulting array is:
Subsection 11.3.5 Practice Pages
You have attempted of activities on this page.