Skip to main content

Section 14.2 Writing-ArrayLists-WE1-P1

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.2.1

Exercises Exercises

1.
Q1: Put the code in the appropriate order so that an ArrayList of integers will store multiples of 5 (from 100 down to 0) in reverse order.
2.
Q2. Which of the following would it be easier to use an initializer list for versus a loop when creating an ArrayList? (Select all that are appropriate)
  • Initialize an ArrayList of author names called FavoriteAuthors
  • Initialize an ArrayList of numbers from 100 to 1000 called NineHundred
  • Initialize an ArrayList of ribbon colors called RibbonColors
  • Initialize an ArrayList with the top ten fastest times for your school’s track team in the 100 meter race named TopTen
  • Initialize an ArrayList with the height (in inches) of all the children in a classroom called ClassroomHeights
3.
Q3. Suppose you are writing a method that adds one to all of the elements of an ArrayList named numbers. Which of the following code snippets would be best for this task?
  • public void addOne(ArrayList<Integer> numbers) {
        for (int i = 0; i < numbers.size(); i++) {
            numbers.set(i, numbers.get(i) + 1);
        }
    }
    
  • public void addOne(ArrayList<Integer> numbers) {
        for (int num : numbers) {
            num++;
        }
    }
    
  • This does not modify the original list because it only changes a copy of each value.
  • public void addOne(ArrayList<Integer> numbers) {
        for (int num : numbers) {
            numbers.set(num, num + 1);
        }
    }
    
  • This misuses num as both index and value.
  • public void addOne(ArrayList<Integer> numbers) {
        for (int i = 0; i < numbers.size(); i++) {
            numbers.set(i, i + 1);
        }
    }
    
  • This sets each element to its index plus one, not to the original value plus one.
You have attempted 1 of 3 activities on this page.