Section 13.7 Worked Example: ArrayLists - Sum
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.7.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 x : alpha)
sum += x;
Subsection 13.7.2 SG1: Declaring and initialization of ArrayList
By just looking at this code you are unable to determine the exact data type of the collection being traversed. This is done on purpose - you can traverse any collection in Java using the exact same syntax! So the important thing to notice is that the enhanced for loop is accessing each element of the collection and adding its value to the variable sum.
This code will sum all the values in the container and is the same as in Section 11.17.
Subsection 13.7.3
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;
if (alpha.size() != 0) {
for (int x : alpha)
sum = sum + x;
avg = (sum * 1.0)/alpha.size();
}
Subsection 13.7.4 SG2: Determine access or action
By just looking at this code you are unable to determine the exact data type of the collection being traversed.
Subsection 13.7.5 Evaluating code
The code first checks to see if there are any values in the ArrayList. If there are, then the enhanced for loop is accessing each element of the collection and adding its value to the variable sum. The average is calculated by dividing the sum by the number of elements in the collection. (Remember that an int divided by an int is an int so to perform double division we need to ensure that one of the operands to division is a double which we accomplish by multiplying the variable sum by 1.0).
If there are no values in alpha, the value in the variable avg is 0.0.
What does this code accomplish?
Subsection 13.7.6 Practice Pages
You have attempted 1 of 1 activities on this page.