Skip to main content

Section 13.3 Worked Example: ArrayList - 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.3.1

Problem:
Given the initialized ArrayList:
ArrayList<Integer> alpha;
Evaluate these statements and determine the value of all elements. If any error occurs, give the reason.
alpha = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
    alpha.add(i * 10);

Subsection 13.3.2 SG1: Declaring and Initialization of ArrayList

alpha = new ArrayList<Integer>();
  • alpha is declared as an ArrayList<Integer>.
  • This statement creates an empty list that can hold Integer objects.
  • The list grows dynamically with each add() call, unlike arrays which have fixed length.

Subsection 13.3.3 SG2: Determine Access or Action

In this example, an ArrayList is declared and instantiated to hold 10 new Integer values. Within the loop, we will be adding values to the list, so we look to SG4.

Subsection 13.3.4 SG4: Change ArrayList Element

There is a loop within the example. Looking at the loop header:
for (int i = 0; i < 10; i++)
The loop control variable (i) will iterate from the value 0 to the value 9. So there will be 10 total iterations. For each iteration, the statement:
alpha.add(i * 10);
is executed. This adds a new value to the end of the ArrayList with each loop iteration.
  • When i is 0, 0 is added to the list.
  • When i is 1, 10 is added to the list.
  • This pattern continues until i is 9, where 90 is added to the list.
The resulting ArrayList contains 10 elements: 0, 10, 20, ..., 90.
Figure 13.3.1.

Subsection 13.3.5 Practice Pages

You have attempted 1 of 2 activities on this page.