Implementing ArrayList Algorithms - AP Computer Science A
- What method adds an element to an ArrayList at a specified index?
- add(index, element). Inserts element at specified position, shifting subsequent elements right.
- What method adds all elements of one ArrayList to another?
- addAll(Collection<? extends E> c). Appends all elements from another collection to current ArrayList.
- How do you find the size of an ArrayList?
- size(). Returns current number of elements stored in the ArrayList.
- Which method replaces an element at a specified index in an ArrayList?
- set(index, element). Replaces existing element at specified position with new element.
- What method returns an array containing all elements of an ArrayList?
- toArray(). Converts ArrayList to standard array containing all elements.
- Identify the import statement for using ArrayList in Java.
- import java.util.ArrayList;. Required import statement to use ArrayList class in Java programs.
- 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.
- 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).
- Which method removes an element from an ArrayList by index?
- remove(index). Removes element at specified position, shifting subsequent elements left.
- What method retrieves an element from an ArrayList by index?
- get(index). Directly accesses element at specified position without modification.
- 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.
- Which method checks if an ArrayList is empty?
- isEmpty(). Returns true if ArrayList contains no elements (size is zero).
- Identify the method to shuffle elements in an ArrayList.
- Collections.shuffle(list). Randomly reorders elements using Collections utility method.
- Find the time complexity of removing an element by index from an ArrayList.
- O(n). Linear time due to shifting all subsequent elements left.
- How do you reverse the elements in an ArrayList?
- Collections.reverse(list). Uses Collections utility to reverse element order in place.
- Identify the method to sort elements in an ArrayList.
- Collections.sort(list). Uses Collections utility class to sort elements in ascending order.
- Identify the method that returns an iterator over the elements in an ArrayList.
- iterator(). Provides forward-only traversal through ArrayList elements.
- 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.
- What method copies elements from one ArrayList to another?
- Collections.copy(dest, src). Copies elements from source to destination ArrayList using Collections.
- What method checks if an ArrayList contains a specific element?
- contains(element). Returns true if ArrayList contains at least one instance of element.
- 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.
- Identify the method to add an element to the end of an ArrayList.
- add(element). Appends element to the end of the ArrayList.
- What is the time complexity of accessing an element by index in an ArrayList?
- O(1). Constant time since ArrayList uses array indexing internally.
- 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.
- 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.
- 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).
- What method removes the first occurrence of a specified element in an ArrayList?
- remove(Object o). Removes first matching element based on equals() comparison.
- Identify the method to clear all elements in an ArrayList.
- clear(). Removes all elements from the ArrayList, making it empty.
- 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.