Skip to main content

Section 14.5 Worked Example: Write ArrayLists - Reverse

Subgoals for Evaluating ArrayLists.

  1. Importing the ArrayList class
    1. Before using ArrayList, import it from the java.util package:
      import java.util.ArrayList;
  2. Declaring an ArrayList variable
    1. Determine the type of objects to be stored (use wrapper classes for primitives, e.g., Integer instead of int)
    2. Determine the name of the ArrayList variable
    3. Use syntax: ArrayList<DataType> name;
  3. Instantiating an ArrayList object
    1. Use the new keyword with the constructor to create a new ArrayList object
    2. Assign to variable using: name = new ArrayList<>();
    3. Optionally, initialize with values using: new ArrayList<>(List.of(value1, value2, ...)); (Java 9+)
  4. Accessing an element in an ArrayList
    1. Determine the index of the element to be accessed
    2. Use: listName.get(index) to retrieve the element
    3. Ensure the index is within bounds: 0 to listName.size() - 1, otherwise an IndexOutOfBoundsException occurs
  5. Changing a value in an ArrayList
    1. Determine the index of the element to be changed
    2. Determine the new value or expression to assign
    3. Use: listName.set(index, newValue) to update the value
  6. Traversing an ArrayList
    1. Decide whether accessing all elements, updating, or accessing a subset
    2. If accessing only, use an enhanced for (for-each) loop:
      1. for (DataType item : listName) - iterates from first to last, storing a copy of each element in item
    3. If updating or using indices, use a traditional for loop:
      1. Initialize loop control variable to 0 (or listName.size() - 1 for reverse)
      2. Set condition: i < listName.size() (or i >= 0 for reverse)
      3. Increment or decrement loop control variable appropriately
    4. Use listName.get(i) to access or listName.set(i, newValue) to update
  7. Whole list actions
    1. Passing an ArrayList as an argument
      1. Check if the method expects an ArrayList argument (check documentation or method signature)
      2. Pass the ArrayList variable in the method call
      3. Note: changes to the ArrayList in the method will persist (objects are passed by reference)
    2. Reassigning an ArrayList
      1. Determine that the reference to the list should point to a new ArrayList
      2. Left-hand side is the variable name of the original list
      3. Right-hand side is the new ArrayList, e.g., new ArrayList<>()

Subsection 14.5.1 Solution 1 – copy to new list with reverse traverse

We will begin with a sample ArrayList called original, and another list of the same size called copy.
import java.util.ArrayList;
import java.util.Collections;

ArrayList<Integer> original = new ArrayList<>();
Collections.addAll(original, 2, 4, 6, 8, 10);
ArrayList<Integer> copy = new ArrayList<>();
for (int i = 0; i < original.size(); i++) {
    copy.add(0);  // placeholder values
}
Then we loop in reverse through the original list, copying the elements in order and placing them starting at the beginning of the new list. The variable place helps us with traversing the new list in the opposite direction of our reversed traversal of the original.
int place = 0;
for (int i = original.size() - 1; i >= 0; i--) {
    copy.set(place++, original.get(i));
}

Subsection 14.5.2 Solution 2 – same list with swaps

In this solution, we do not need the whole extra list in memory. The loop only traverses half of the list, swapping the value at i with the value at the mirrored/reflected position list.size() - i - 1.
for (int i = 0; i < list.size() / 2; i++) {
    int temp = list.get(i);
    list.set(i, list.get(list.size() - i - 1));
    list.set(list.size() - i - 1, temp);
}

Subsection 14.5.3 Practice Pages

You have attempted 1 of 1 activities on this page.