Implementing ArrayList Algorithms - AP Computer Science A

  1. What method adds an element to an ArrayList at a specified index?
    add(index, element). Inserts element at specified position, shifting subsequent elements right.
  2. What method adds all elements of one ArrayList to another?
    addAll(Collection<? extends E> c). Appends all elements from another collection to current ArrayList.
  3. How do you find the size of an ArrayList?
    size(). Returns current number of elements stored in the ArrayList.
  4. Which method replaces an element at a specified index in an ArrayList?
    set(index, element). Replaces existing element at specified position with new element.
  5. What method returns an array containing all elements of an ArrayList?
    toArray(). Converts ArrayList to standard array containing all elements.
  6. Identify the import statement for using ArrayList in Java.
    import java.util.ArrayList;. Required import statement to use ArrayList class in Java programs.
  7. Identify the method to sort elements in an ArrayList using a comparator.
    sort(Comparator<? super E> c). Sorts using custom comparison logic defined by Comparator implementation.
  8. What is the time complexity of adding an element at the end of an ArrayList?
    O(1) on average. Constant time unless internal array needs resizing (rare case).
  9. Which method removes an element from an ArrayList by index?
    remove(index). Removes element at specified position, shifting subsequent elements left.
  10. What method retrieves an element from an ArrayList by index?
    get(index). Directly accesses element at specified position without modification.
  11. Which method returns the last index of a specified element in an ArrayList?
    lastIndexOf(element). Finds rightmost position of element, or -1 if not found.
  12. Which method checks if an ArrayList is empty?
    isEmpty(). Returns true if ArrayList contains no elements (size is zero).
  13. Identify the method to shuffle elements in an ArrayList.
    Collections.shuffle(list). Randomly reorders elements using Collections utility method.
  14. Find the time complexity of removing an element by index from an ArrayList.
    O(n). Linear time due to shifting all subsequent elements left.
  15. How do you reverse the elements in an ArrayList?
    Collections.reverse(list). Uses Collections utility to reverse element order in place.
  16. Identify the method to sort elements in an ArrayList.
    Collections.sort(list). Uses Collections utility class to sort elements in ascending order.
  17. Identify the method that returns an iterator over the elements in an ArrayList.
    iterator(). Provides forward-only traversal through ArrayList elements.
  18. How do you create an ArrayList of Strings named 'list'?
    ArrayList<String> list = new ArrayList<>();. Creates new ArrayList with String generic type using diamond operator.
  19. What method copies elements from one ArrayList to another?
    Collections.copy(dest, src). Copies elements from source to destination ArrayList using Collections.
  20. What method checks if an ArrayList contains a specific element?
    contains(element). Returns true if ArrayList contains at least one instance of element.
  21. How do you convert an ArrayList to an array of Strings?
    list.toArray(new String[0]). Converts to String array with proper type safety using parameterized method.
  22. Identify the method to add an element to the end of an ArrayList.
    add(element). Appends element to the end of the ArrayList.
  23. What is the time complexity of accessing an element by index in an ArrayList?
    O(1). Constant time since ArrayList uses array indexing internally.
  24. What method swaps two elements at specified positions in an ArrayList?
    Collections.swap(list, i, j). Exchanges elements at positions i and j using Collections utility.
  25. Find the index of the first occurrence of an element in an ArrayList.
    indexOf(element). Returns position of first matching element, or -1 if not found.
  26. What method returns a sublist of an ArrayList from index 'from' to 'to'?
    subList(int from, int to). Returns view of portion between indices (exclusive of 'to' index).
  27. What method removes the first occurrence of a specified element in an ArrayList?
    remove(Object o). Removes first matching element based on equals() comparison.
  28. Identify the method to clear all elements in an ArrayList.
    clear(). Removes all elements from the ArrayList, making it empty.
  29. Identify the method to replace all elements in an ArrayList with a specified value.
    Collections.fill(list, value). Uses Collections utility to replace all elements with single value.

Study these flashcards