Section 13.11 Worked Example: ArrayLists - Average
Subgoals for Evaluating ArrayLists.
-
Declaration and initialization of an ArrayList
-
Set up a one-dimensional table that will either be empty or have a specified initial capacity based on the parameter to the constructor
-
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)
-
Upon instantiation, an ArrayList contains zero elements initially, but elements can be added dynamically using
add()
-
-
Determine access or change of an element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
-
Accessing an ArrayList element
-
Determine the value of the parameter in the
get(<expression>)
method call (remember evaluating expressions subgoals) -
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. -
Index must be between 0 and
arrayListName.size() - 1
, inclusive; otherwiseIndexOutOfBoundsException
occurs -
arrayListName.get(index)
returns the value stored at that index
-
-
Changing value of an ArrayList element
-
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 -
(remember the assignment subgoals for verifying data types and evaluating expressions)
-
(remember rules for index values)
-
-
Whole ArrayList actions
-
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.
-
Assignment - changes the reference to point to the ArrayList on the right-hand side of the assignment operator.
-
Subsection 13.11.1
Problem: Assume that the Integer ArrayList
alpha
has been properly declared and initialized with non-default values. What does this code accomplish?
int sum = 0;
double avg = 0.0;
for (int i = 0; i < alpha.size(); i++)
sum = sum + alpha.get(i);
if (alpha.size() != 0)
avg = (sum * 1.0) / alpha.size();
Subsection 13.11.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.

-
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.11.3 SG2: Determine access or action
Within the loop, we are accessing ArrayList elements.
Subsection 13.11.4 Evaluating code
int sum = 0;
double avg = 0.0;
The first two lines are declaring two variables, an integer to hold the sum of all the values in the ArrayList and a double to hold the average of the values.
References to the ArrayList are in the loop and selection statements:
for (int i = 0; i < alpha.size(); i++)
sum = sum + alpha.get(i);
This is the same loop from the Section 13.7 example.
if (alpha.size() != 0)
avg = (sum * 1.0) / alpha.size();
-
If the size of the ArrayList is not 0, then it is divided by the number of values in alpha.
-
All indexes into the ArrayList are valid, and all assignments are valid.
Let us trace with a sample ArrayList.

First,
sum
and avg
are both initialized to zero values, and then a for-loop is used to traverse the ArrayList to calculate the sum. 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.

Next we look at the selection statement:
if (alpha.size() != 0)
avg = (sum * 1.0) / alpha.size();
Why have the selection statement? What if the ArrayList has been declared, but has no values? Then its size is 0 – and we would be dividing by 0! An exception! So we guard against this by checking the size.
Why do we need to multiply sum by 1.0? Remember, an int divided by an int is always an int! Our sample did not have any remainder or decimal value, but that might not always be the case. So we need to make sure that either the divisor or dividend is a double; and one way to do this without affecting the value is to multiply it by 1.0. Another way would be to add 0.0 to the value. Still another way would be to cast the value as a double.
So an alternate equivalent code might be:
if (alpha.size() != 0)
avg = (double) sum / alpha.size();
What does this code accomplish?
Subsection 13.11.5 Practice Pages
You have attempted 1 of 1 activities on this page.