ArrayList Traversals - AP Computer Science A

  1. Identify the index of the first element in an ArrayList.
    0. ArrayLists use zero-based indexing like arrays.
  2. State the method to sort elements in an ArrayList using natural ordering.
    Collections.sort(arrayList). Sorts elements using their natural comparison order.
  3. What loop structure uses an iterator to traverse an ArrayList?
    Iterator loop. Uses Iterator object for controlled sequential access.
  4. Identify a method to get the size of an ArrayList.
    ArrayList.size(). Returns number of elements currently stored in the list.
  5. What is the return type of ArrayList.size()?
    int. Returns integer count of elements in the ArrayList.
  6. Identify the method to retain only elements in an ArrayList that are contained in another collection.
    ArrayList.retainAll(collection). Keeps only elements present in both collections.
  7. Write the for-each loop syntax to traverse an ArrayList of integers.
    for (Integer num : arrayList) { }. Enhanced for loop iterates through each Integer in the list.
  8. What exception is thrown when accessing an invalid index in an ArrayList?
    IndexOutOfBoundsException. Runtime exception for accessing non-existent index positions.
  9. Identify the method to add an element to the end of an ArrayList.
    ArrayList.add(element). Appends element to the end of the list.
  10. What is the purpose of traversing an ArrayList?
    To access each element in the ArrayList. Allows examination or processing of every element sequentially.
  11. Find the syntax to initialize an empty ArrayList of integers.
    ArrayList<Integer> list = new ArrayList<>();. Creates new empty ArrayList with Integer type parameter.
  12. Identify the keyword to break out of a loop early.
    break. Immediately exits loop when condition is met.
  13. Which method retrieves an element from an ArrayList at a specific index?
    ArrayList.get(index). Returns element at specified position without removing it.
  14. Write the syntax to traverse an ArrayList using an iterator.
    while (iterator.hasNext()) { iterator.next(); }. Uses iterator methods to safely traverse elements.
  15. Identify the method to add all elements from one ArrayList to another.
    ArrayList.addAll(collection). Appends all elements from source collection.
  16. Choose the method to replace all elements in an ArrayList with specified elements.
    ArrayList.replaceAll(unaryOperator). Applies function to transform each element in place.
  17. Which method replaces an element at a specific index in an ArrayList?
    ArrayList.set(index, element). Updates element at specified index position.
  18. What is the time complexity of removing an element from the beginning of an ArrayList?
    O(n). Requires shifting all subsequent elements leftward.
  19. Check if an ArrayList contains a specific element.
    ArrayList.contains(element). Returns boolean indicating element presence in the list.
  20. What is the time complexity of accessing an element by index in an ArrayList?
    O(1). Direct array access provides constant time retrieval.
  21. Find the syntax to declare an ArrayList of strings with initial capacity.
    ArrayList<String> list = new ArrayList<>(capacity);. Pre-allocates memory for specified number of elements.
  22. What is the time complexity of adding an element at the end of an ArrayList?
    O(1). Appending to end requires no element shifting.
  23. What method of Iterator checks for more elements?
    Iterator.hasNext(). Returns true if more elements remain to iterate.
  24. State the method to find the last index of an element in an ArrayList.
    ArrayList.lastIndexOf(element). Returns highest index where element appears.
  25. Given an ArrayList of strings, write a loop to print each element.
    for (String s : arrayList) { System.out.println(s); }. Enhanced for loop prints each string element sequentially.
  26. Identify the method to obtain an iterator for an ArrayList.
    ArrayList.iterator(). Returns Iterator object for traversing the ArrayList.
  27. Identify the method to check if an ArrayList is empty.
    ArrayList.isEmpty(). Returns boolean indicating if size equals zero.
  28. Identify the method to remove an element by index from an ArrayList.
    ArrayList.remove(index). Removes element at specified position and returns it.
  29. What is a common error when accessing elements during an ArrayList traversal?
    ConcurrentModificationException. Thrown when modifying ArrayList during iteration.
  30. What type of loop is commonly used to traverse an ArrayList?
    For-each loop. Enhanced for loop simplifies iteration without index management.
  31. What is the return type of ArrayList.get(index)?
    Object or specified type. Returns the generic type or Object if unspecified.
  32. State the method to remove the first occurrence of an element from an ArrayList.
    ArrayList.remove(element). Searches and removes first matching element found.
  33. What method checks if two ArrayLists contain the same elements in the same order?
    ArrayList.equals(object). Compares both content and order of elements.
  34. What is the purpose of traversing an ArrayList using an enhanced for loop?
    To simplify iteration over elements. Enhanced for loop provides cleaner, safer iteration.
  35. Find the index of the last element in an ArrayList of size nn.
    n−1n - 1. Last index equals size minus one due to zero-based indexing.
  36. Choose the method to convert an ArrayList to an array.
    ArrayList.toArray(). Converts ArrayList to standard array format.
  37. What is the time complexity of iterating over an ArrayList using a for-each loop?
    O(n). Must visit each element once for complete traversal.
  38. State the method to clear all elements from an ArrayList.
    ArrayList.clear(). Removes all elements, making the ArrayList empty.
  39. Choose the method to add an element at a specific index in an ArrayList.
    ArrayList.add(index, element). Inserts element at specified position, shifting existing elements.

Study these flashcards