Skip to main content

Section 13.6 Worked Example: ArrayLists - Initializer and Reverse Traverse

Subgoals for Evaluating ArrayLists.

  1. Declaring and initialization of an ArrayList
    1. Set up a one-dimensional dynamic list (initially empty or with a specified initial capacity)
    2. 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.
  2. Determine access or change of element, or action on entire ArrayList object, and update elements as needed (remembering assignment subgoals)
  3. Accessing an ArrayList element
    1. Evaluate expression within get(index) which will be the index for the element to be accessed
    2. arrayListName.get(index) returns the value stored at that index
    3. index must be between 0 and arrayListName.size() - 1, inclusive; otherwise IndexOutOfBoundsException occurs
  4. Changing value of an ArrayList element
    1. Evaluate expression within set(index, value) which will be the index for the element to be replaced
    2. arrayListName.set(index, value) replaces the element at index with the specified value
    3. (remember the assignment subgoals for verifying data types and evaluating expressions)
    4. (remember rules for index values)
  5. Whole ArrayList actions
    1. 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.
    2. 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));
  • alpha is declared as an ArrayList<Integer>.
  • This statement creates an ArrayList with 7 elements using Arrays.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.
  • Each element is accessed with get(i), incremented by 1, and stored back with set(i, ...).
  • 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
Answer.
7
14 1 -3 7 8 25 16

Subsection 13.6.5 Practice Pages

You have attempted 1 of 2 activities on this page.