Skip to main content

Section 13.9 Worked Example: ArrayLists - Sum

Subgoals for Evaluating ArrayLists.

  1. Declaring and initialization of an ArrayList
    1. Set up a one-dimensional dynamic list (initially empty or with a specified initial capacity)
    2. Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using add(). Elements not yet added do not exist until explicitly inserted.
  2. Determine access or change of element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
  3. Accessing an ArrayList element
    1. Evaluate expression within get(index) which will be the index for the element to be accessed
    2. arrayListName.get(index) returns the value stored at that index
    3. index must be between 0 and arrayListName.size() - 1, inclusive; otherwise IndexOutOfBoundsException occurs
  4. Changing value of an ArrayList element
    1. Evaluate expression within set(index, value) which will be the index for the element to be replaced
    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 exception is if the argument is assigned to reference a different ArrayList inside the method.
    2. Assignment - changes the reference to point to the ArrayList on the right-hand side of the assignment operator.

Subsection 13.9.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 = 0; i < alpha.size(); i++) {
    sum += alpha.get(i);
}

Subsection 13.9.2 SG1: Declaring and initialization of ArrayList

There is no explicit declaration or initialization of an ArrayList within the code. However, within the code there are .get() method calls, so we know we are accessing an ArrayList.
Figure 13.9.1.
  • alpha is an ArrayList of Integers and has values, but we don’t know what those values are
  • however, we can still diagram a representation of this ArrayList
  • notice that the largest index is size - 1

Subsection 13.9.3 SG2: Determine access or action

Within the loop, we are accessing ArrayList elements.

Subsection 13.9.4 Evaluating code

for (int i = 0; i < alpha.size(); i++) {
   sum += alpha.get(i);
}
In Java, ArrayLists have a method called size() which returns the number of elements in the ArrayList. To access the size of the ArrayList you use arraylist_name.size().
  • This loop has index i go from 0 to size - 1 (<size()) by increments of 1.
  • Then the value at alpha.get(i) is added to sum.
  • All indexes into the ArrayList are valid, and all assignments are valid.
  • Notice that alpha.size() -1 is the same value as size - 1.
Let us trace with a sample ArrayList.
Figure 13.9.2.
The first line of the code sample initializes sum to a have a value of zero, and 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.9.3.
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.

Subsection 13.9.5 Practice Pages

You have attempted 1 of 1 activities on this page.