Skip to main content

Section 13.15 Worked Example: Enhanced For Loops

Subgoals for Evaluating ArrayLists.

  1. Declaration and initialization of an ArrayList
    1. Set up a one-dimensional table that will either be empty or have a specified initial capacity based on the parameter to the constructor
    2. When declaring an ArrayList, the datatype stored in the container is specified inside of <>, and the data type must be the name of a class (no primitive data types)
    3. Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using add()
  2. Determine access or change of an element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
  3. Accessing an ArrayList element
    1. Determine the value of the parameter in the get(<expression>) method call (remember evaluating expressions subgoals)
    2. The parameter to the method get represents the index in the ArrayList. The size of the ArrayList is the number of elements contained. If the ArrayList is initially empty, the size is 0.
    3. Index must be between 0 and arrayListName.size() - 1, inclusive; otherwise IndexOutOfBoundsException occurs
    4. arrayListName.get(index) returns the value stored at that index
  4. Changing value of an ArrayList element
    1. Determine the value of the first parameter in the set(<expression>, value) method call which will be the index for the element to be updated
    2. arrayListName.set(index, value) replaces the element at index with the specified value
    3. (remember the assignment subgoals for verifying data types and evaluating expressions)
    4. (remember rules for index values)
  5. Whole ArrayList actions
    1. Passing as argument - a copy of the reference to the instantiated ArrayList is passed to the method. This means that any changes made to the elements inside the method persist outside the method. The one exception to this is if you assign the argument to reference a different ArrayList in memory.
    2. Assignment - changes the reference to point to the ArrayList on the right-hand side of the assignment operator.

Subsection 13.15.1

Problem: Assume that the integer ArrayList 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 13.15.2 SG1: Declaring and initialization of ArrayList

There is no explicit declaration or initialization of an ArrayList within the code. There aren’t even any < > to signal an ArrayList! However, in Java you can use an enhanced for loop, also known as a for-each loop, to iterate through a collection. And an ArrayList 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 ArrayList 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 13.15.3 SG2: Determine access or action

Because it is an enhanced for loop, we are accessing ArrayList elements.

Subsection 13.15.4

int sum = 0;
for (int i : alpha) {
    sum += i;
}
This code example is equivalent to the code used in the SectionΒ 13.7 example; it will sum the values of the ArrayList.
Let us trace the code with a simple ArrayList.
Figure 13.15.1.
The first line of the code sample initializes sum to have a value of zero. Then, a for-loop is used to traverse the ArrayList. 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.
Figure 13.15.2.
The final value of sum is 59, which is the sum of all values in the ArrayList.
What does this code accomplish?
Answer.
sum contains the sum of all the values in the ArrayList alpha.
You have attempted of activities on this page.