Section 13.11 Worked Example: ArrayLists - Minimum Value
Subgoals for Evaluating ArrayLists.
-
Declaring and initialization of an ArrayList
-
Set up a one-dimensional dynamic list (initially empty or with a specified initial capacity)
-
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.
-
-
Determine access or change of element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
-
Accessing an ArrayList element
-
Evaluate expression within
get(index)
which will be the index for the element to be accessed -
arrayListName.get(index)
returns the value stored at that index -
index must be between 0 and
arrayListName.size() - 1
, inclusive; otherwiseIndexOutOfBoundsException
occurs
-
-
Changing value of an ArrayList element
-
Evaluate expression within
set(index, value)
which will be the index for the element to be replaced -
(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 exception is if the argument is assigned to reference a different ArrayList inside the method.
-
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 min = alpha.get(0);
for (int i = 1; i < alpha.size(); i++) {
if (alpha.get(i) < min)
min = alpha.get(i);
}
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 (three different places!), 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 min = alpha.get(0);
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
.
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()
.
for (int i = 1; i < alpha.size(); i++) {
if (alpha.get(i) < min)
min = alpha.get(i);
}
-
Then the value at
alpha.get(i)
is compared tomin
. If the value atalpha.get(i)
is less thanmin
, thenalpha.get(i)
is copied intomin
. -
All indexes into the ArrayList are valid, and all assignments are valid.
Let us trace with a sample ArrayList.

The first line of the code sample initializes
min
to copy the value from alpha.get(0)
, which in our sample is 12, 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 one.

We can see that each time a smaller value is located in the ArrayList, that value is stored in
min
.
What does this code accomplish?
Subsection 13.11.5 Practice Pages
You have attempted 1 of 1 activities on this page.