Skip to main content

Section 14.4 Writing-ArrayLists-WE2-P1

Subgoals for Writing 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 (When ArrayLists are instantiated, they are empty and have a size of 0.)
  4. Adding elements to an ArrayList
    1. To add to the end of an ArrayList, use: listName.add(valueToBeAdded)
    2. To add an element at a specific location in an ArrayList, use: listName.add(index, valueToBeAdded) where index is within the bounds 0 to listName.size()
  5. 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
  6. 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
    4. Ensure the index is within bounds: 0 to listName.size() - 1, otherwise an IndexOutOfBoundsException occurs
  7. Traversing an ArrayList
    1. Decide whether accessing all elements, updating, or accessing a subset
    2. If only accessing elements, 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 Subgoals 5 or 6 to access or change values as appropriate
  8. 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. When calling a method, pass a reference to an ArrayList (usually variable name) as an argument in the method call.
      3. Note: changes to elements in the ArrayList that are done inside the method will persist

Subsection 14.4.1

Exercises Exercises

1.
Q4: What are the contents of alpha after this code has been executed?
import java.util.ArrayList;

ArrayList<Integer> alpha = new ArrayList<>();
Collections.addAll(alpha, 10, 20, 30, 40, 50, 60, 70);

int start = alpha.get(alpha.size() - 1);
for (int i = 1; i < alpha.size(); i++) {
    alpha.set(i, alpha.get(i - 1));
}
alpha.set(0, start);
  • {20, 30, 40, 50, 60, 70, 10}
  • Incorrect
  • {10, 20, 30, 40, 50, 60, 70}
  • Incorrect
  • {10, 10, 20, 30, 40, 50, 60}
  • Incorrect
  • {70, 10, 10, 10, 10, 10, 10}
  • Correct
  • {10, 10, 10, 10, 10, 10, 10}
  • Incorrect
2.
Q5: What are the contents of beta after this code has been executed?
import java.util.ArrayList;

ArrayList<Integer> beta = new ArrayList<>();
Collections.addAll(beta, 10, 20, 30, 40, 50, 60, 70);

for (int i = 1; i < beta.size(); i++) {
    beta.set(i, beta.get(i - 1));
}
  • {20, 30, 40, 50, 60, 70, 10}
  • Incorrect
  • {10, 20, 30, 40, 50, 60, 70}
  • Incorrect
  • {10, 10, 20, 30, 40, 50, 60}
  • Incorrect
  • {70, 10, 20, 30, 40, 50, 60}
  • Incorrect
  • {10, 10, 10, 10, 10, 10, 10}
  • Correct
3.
Q6: What happens when this code is executed on gamma?
import java.util.ArrayList;

ArrayList<Integer> gamma = new ArrayList<>();
Collections.addAll(gamma, 10, 20, 30, 40, 50, 60, 70);

for (int i = 0; i < gamma.size(); i++) {
    gamma.set(i, gamma.get(i + 1));
}
  • {20, 30, 40, 50, 60, 70, 70}
  • Incorrect
  • {20, 30, 40, 50, 60, 70, 10}
  • Incorrect
  • {10, 20, 30, 40, 50, 60, 70}
  • Incorrect
  • {10, 10, 20, 30, 40, 50, 60}
  • Incorrect
  • IndexOutOfBounds Exception
  • Correct
4.
Q7: Put the following code in order so that the values inserted into the ArrayList are stored in increasing order.
You have attempted 1 of 4 activities on this page.