Section 13.6 Worked Example: ArrayLists - Initializer and Reverse Traverse
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.6.1
Problem: Evaluate these statements and determine their output
import java.util.ArrayList;
import java.util.Arrays;
ArrayList<Integer> alpha = new ArrayList<>(Arrays.asList(15, 24, 7, 6, -4, 0, 13));
System.out.println(alpha.size());
for (int i = alpha.size() - 1; i >= 0; i--)
alpha.set(i, alpha.get(i) + 1);
for (int i = alpha.size() - 1; i >= 0; i--)
System.out.print(alpha.get(i) + " ");
Subsection 13.6.2 SG1: Declaring and initialization of ArrayList
ArrayList<Integer> alpha = new ArrayList<>(Arrays.asList(15, 24, 7, 6, -4, 0, 13));
-
This statement creates an
ArrayList
with 7 elements usingArrays.asList()
to initialize with literal values. -
ArrayLists do not use default values like arrays; instead, the provided initializer values directly define the contents of the list.
Subsection 13.6.3 SG2: Determine access or action
Within the loop, there is both access (SG3) and modification (SG4) of elements. The actions are on individual elements using
get(i)
and set(i, value)
.
Subsection 13.6.4 Evaluating code
System.out.println(alpha.size());
This line prints the size of the ArrayList, which is
7
, followed by a newline.
Then, this loop:
for (int i = alpha.size() - 1; i >= 0; i--)
alpha.set(i, alpha.get(i) + 1);
-
Iterates from index 6 to 0, inclusive.
-
Only valid integer operations are performed, and no out-of-bounds access occurs.
The resulting ArrayList is:
[16, 25, 8, 7, -3, 1, 14]
The second loop prints the elements in reverse order:
for (int i = alpha.size() - 1; i >= 0; i--)
System.out.print(alpha.get(i) + " ");
This outputs: 14 1 -3 7 8 25 16
Subsection 13.6.5 Practice Pages
You have attempted 1 of 2 activities on this page.