Skip to main content

Section 13.1 Worked Example: ArrayLists - Instantiate and Alter

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.1.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>(Collections.nCopies(5, 0));
alpha.set(4, 22);
alpha.set(0, 10);
alpha.set(1, alpha.get(4) - alpha.get(0));
alpha.set(2, alpha.get(1) - alpha.get(0));
alpha.set(3, alpha.get(alpha.get(2) - 1));
alpha.set(4, alpha.get(alpha.get(3)));

Subsection 13.1.2 SG1: Declaration and initialization of ArrayList

alpha = new ArrayList<Integer>(Collections.nCopies(5, 0));
  • alpha is declared as an ArrayList of Integers.
  • This statement initializes alpha with 5 elements, all set to 0 initially.
  • The valid index values are from 0 to 4.
  • We use Collections.nCopies to fill the ArrayList with default values of 0 (since integers in Java must be explicitly initialized).

Subsection 13.1.3 SG2: Determine access or action

alpha.set(4, 22);
alpha.set(0, 10);
alpha.set(1, alpha.get(4) - alpha.get(0));
alpha.set(2, alpha.get(1) - alpha.get(0));
alpha.set(3, alpha.get(alpha.get(2) - 1));
alpha.set(4, alpha.get(alpha.get(3)));
When alpha.set(index, value) is used, we are updating an element (SG4). When alpha.get(index) is used, we are accessing an element (SG3).
We will walk through the steps line by line, just as with arrays.

Subsection 13.1.4 SG3: Update elements

alpha.set(4, 22);
alpha.set(0, 10);
We update the value at index 4 to 22, and index 0 to 10. This is valid as both indices are within bounds (0–4).
Now alpha contains:
[10, 0, 0, 0, 22]

Subsection 13.1.5 SG4: Compute using get()

alpha.set(1, alpha.get(4) - alpha.get(0));
Evaluate alpha.get(4) - alpha.get(0):
22 - 10 = 12
We set index 1 to 12. Now alpha contains:
[10, 12, 0, 0, 22]

Subsection 13.1.6 SG5: Continue computations

alpha.set(2, alpha.get(1) - alpha.get(0));
Evaluate alpha.get(1) - alpha.get(0):
12 - 10 = 2
We set index 2 to 2. Now alpha contains:
[10, 12, 2, 0, 22]

Subsection 13.1.7 SG6: Indexed access using value

alpha.set(3, alpha.get(alpha.get(2) - 1));
Evaluate alpha.get(2) = 2, so the expression becomes alpha.get(1) = 12.
We set index 3 to 12. Now alpha contains:
[10, 12, 2, 12, 22]

Subsection 13.1.8 SG7: Error analysis

alpha.set(4, alpha.get(alpha.get(3)));
Evaluate alpha.get(3) = 12, so the expression becomes alpha.get(12).
This will cause an IndexOutOfBoundsException because index 12 is outside the size of the ArrayList.

Subsection 13.1.9 Practice Pages

You have attempted 1 of 2 activities on this page.