Section 11.17 Worked Example: Enhanced For Loops
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.17.1
Problem: Assume that the integer array
alpha
has been properly declared and initialized with non-default values (not all values are 0). What does this code accomplish?int sum = 0;
for (int i : alpha) {
sum += i;
}
Subsection 11.17.2 SG1: Declaring and initialization of array
There is no explicit declaration or initialization of an array within the code. There aren’t even any [ ] to signal an array! However, in Java you can use an enhanced for loop, also known as a for-each loop, to iterate through a collection. And an array is a collection.
Here’s how an enhanced for loop works:
for (datatype varaible : collectionName) {
variable reference...
}
Within the for-each loop header you declare a local variable of the data type held in the collection followed by a colon (:) followed by the name of the collection. This tells the system to iterate through the collection, assigning the value from the collection to the variable. In the first iteration of the loop, the variable will hold the first element of the collection, in the second iteration, the variable will hold the second element of the collection, etc. Notice that you can reference the value of the array element (just the variable name), but you do not have access to the index or position of the element with an enhanced for loop.
Another important thing to remember is that the variable is a copy of the element, not the original element, so updates to the value of the variable have no effect on the values in the collection.
Subsection 11.17.3 SG2: Determine access or action
Because it is an enhanced for loop, we are accessing array elements.
Subsection 11.17.4
int sum = 0;
for (int i : alpha) {
sum += i;
}
This code example is equivalent to the code used in the Section 11.9 example; it will sum the values of the array.
Let us trace the code with a simple array.
The first line of the code sample initializes
sum
to have a value of zero. Then, a for-loop is used to traverse the array. The chart below uses one line to represent the memory and calculations during each iteration of the loop, starting when i
has a value of zero.The final value of
sum
is 59, which is the sum of all values in the array.What does this code accomplish?
Answer.
sum
contains the sum of all the values in the array alpha
.You have attempted of activities on this page.