Section 13.9 Worked Example: ArrayLists - Minimum Value
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.9.1
Problem: Assume that the Integer ArrayList
alpha
has been properly declared and initialized with non-default values. What does this code accomplish?
int min = alpha.get(0);
for (int i : alpha) {
if (i < min)
min = i;
}
Subsection 13.9.2 SG2: Determine access or action
The first line of code contains a
get
call, so we are accessing ArrayList elements. The value of the first element is stored in the variable min
. Because min
is an integer, the ArrayList must also hold integer values.
Subsection 13.9.3 Evaluating code
The first statement is valid because
alpha
stores Integers, and 0 is a valid index. You can assign an Integer value to an int variable such as min
(due to auto-unboxing). This statement is storing the value of the first element in the ArrayList in the variable min
.
for (int i : alpha) {
if (i < min)
min = i;
}
-
This enhanced loop will iterate through each element in alpha copying each ArrayList element to the variable
i
. -
The value stored in
i
is compared tomin
. If the value ini
is less thanmin
, theni
is copied intomin
.
What does this code accomplish?
Subsection 13.9.4
Problem: But what if we need to know the location of where the minimum value is stored? Then we cannot use a for-each loop because we need to know the location or index. So we need to use a regular for loop.
if (alpha.size() > 0) {
int minIndex = 0;
for (int i = 1; i < alpha.size(); i++) {
if (alpha.get(i) < alpha.get(minIndex))
minIndex = i;
}
}
Subsection 13.9.5
Remember that ArrayLists are different from arrays and can actually be "empty". So first we check to see that the ArrayList
alpha
has elements. If alpha
isn’t empty, then we assume that the first element in the ArrayList is the minimum value, so we store 0 as the location of the minimum value.
The
for
loop will begin at index 1 (no need to check value at index 0, as it’s already assumed to be the minimum value) and iterate through each element in alpha
.
The value at index
i
is compared to the value at index minIndex
and if it’s smaller, then the value of i
is stored as the index containing the minimum value (stored in minIndex
). As the algorithm progresses, the variable minIndex
stores the location of the minimum value found so far in the ArrayList.
Subsection 13.9.6 Practice Pages
You have attempted 1 of 1 activities on this page.