Section 13.6 Worked Example: ArrayLists - Initializer and Reverse Traverse
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.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.