ArrayList Traversals - AP Computer Science A
Card 1 of 30
What is a common error when accessing elements during an ArrayList traversal?
What is a common error when accessing elements during an ArrayList traversal?
Tap to reveal answer
ConcurrentModificationException. Thrown when modifying ArrayList during iteration.
ConcurrentModificationException. Thrown when modifying ArrayList during iteration.
← Didn't Know|Knew It →
Choose the method to replace all elements in an ArrayList with specified elements.
Choose the method to replace all elements in an ArrayList with specified elements.
Tap to reveal answer
ArrayList.replaceAll(unaryOperator). Applies function to transform each element in place.
ArrayList.replaceAll(unaryOperator). Applies function to transform each element in place.
← Didn't Know|Knew It →
What is a common error when accessing elements during an ArrayList traversal?
What is a common error when accessing elements during an ArrayList traversal?
Tap to reveal answer
ConcurrentModificationException. Thrown when modifying ArrayList during iteration.
ConcurrentModificationException. Thrown when modifying ArrayList during iteration.
← Didn't Know|Knew It →
Identify the method to retain only elements in an ArrayList that are contained in another collection.
Identify the method to retain only elements in an ArrayList that are contained in another collection.
Tap to reveal answer
ArrayList.retainAll(collection). Keeps only elements present in both collections.
ArrayList.retainAll(collection). Keeps only elements present in both collections.
← Didn't Know|Knew It →
What is the purpose of traversing an ArrayList?
What is the purpose of traversing an ArrayList?
Tap to reveal answer
To access each element in the ArrayList. Allows examination or processing of every element sequentially.
To access each element in the ArrayList. Allows examination or processing of every element sequentially.
← Didn't Know|Knew It →
Choose the method to add an element at a specific index in an ArrayList.
Choose the method to add an element at a specific index in an ArrayList.
Tap to reveal answer
ArrayList.add(index, element). Inserts element at specified position, shifting existing elements.
ArrayList.add(index, element). Inserts element at specified position, shifting existing elements.
← Didn't Know|Knew It →
What is the time complexity of accessing an element by index in an ArrayList?
What is the time complexity of accessing an element by index in an ArrayList?
Tap to reveal answer
O(1). Direct array access provides constant time retrieval.
O(1). Direct array access provides constant time retrieval.
← Didn't Know|Knew It →
State the method to clear all elements from an ArrayList.
State the method to clear all elements from an ArrayList.
Tap to reveal answer
ArrayList.clear(). Removes all elements, making the ArrayList empty.
ArrayList.clear(). Removes all elements, making the ArrayList empty.
← Didn't Know|Knew It →
Find the syntax to initialize an empty ArrayList of integers.
Find the syntax to initialize an empty ArrayList of integers.
Tap to reveal answer
ArrayList list = new ArrayList<>();. Creates new empty ArrayList with Integer type parameter.
ArrayList
← Didn't Know|Knew It →
What loop structure uses an iterator to traverse an ArrayList?
What loop structure uses an iterator to traverse an ArrayList?
Tap to reveal answer
Iterator loop. Uses Iterator object for controlled sequential access.
Iterator loop. Uses Iterator object for controlled sequential access.
← Didn't Know|Knew It →
Identify the method to obtain an iterator for an ArrayList.
Identify the method to obtain an iterator for an ArrayList.
Tap to reveal answer
ArrayList.iterator(). Returns Iterator object for traversing the ArrayList.
ArrayList.iterator(). Returns Iterator object for traversing the ArrayList.
← Didn't Know|Knew It →
What method of Iterator checks for more elements?
What method of Iterator checks for more elements?
Tap to reveal answer
Iterator.hasNext(). Returns true if more elements remain to iterate.
Iterator.hasNext(). Returns true if more elements remain to iterate.
← Didn't Know|Knew It →
Write the syntax to traverse an ArrayList using an iterator.
Write the syntax to traverse an ArrayList using an iterator.
Tap to reveal answer
while (iterator.hasNext()) { iterator.next(); }. Uses iterator methods to safely traverse elements.
while (iterator.hasNext()) { iterator.next(); }. Uses iterator methods to safely traverse elements.
← Didn't Know|Knew It →
What is the time complexity of adding an element at the end of an ArrayList?
What is the time complexity of adding an element at the end of an ArrayList?
Tap to reveal answer
O(1). Appending to end requires no element shifting.
O(1). Appending to end requires no element shifting.
← Didn't Know|Knew It →
Identify the method to add an element to the end of an ArrayList.
Identify the method to add an element to the end of an ArrayList.
Tap to reveal answer
ArrayList.add(element). Appends element to the end of the list.
ArrayList.add(element). Appends element to the end of the list.
← Didn't Know|Knew It →
Which method replaces an element at a specific index in an ArrayList?
Which method replaces an element at a specific index in an ArrayList?
Tap to reveal answer
ArrayList.set(index, element). Updates element at specified index position.
ArrayList.set(index, element). Updates element at specified index position.
← Didn't Know|Knew It →
State the method to remove the first occurrence of an element from an ArrayList.
State the method to remove the first occurrence of an element from an ArrayList.
Tap to reveal answer
ArrayList.remove(element). Searches and removes first matching element found.
ArrayList.remove(element). Searches and removes first matching element found.
← Didn't Know|Knew It →
What is the return type of ArrayList.get(index)?
What is the return type of ArrayList.get(index)?
Tap to reveal answer
Object or specified type. Returns the generic type or Object if unspecified.
Object or specified type. Returns the generic type or Object if unspecified.
← Didn't Know|Knew It →
Choose the method to convert an ArrayList to an array.
Choose the method to convert an ArrayList to an array.
Tap to reveal answer
ArrayList.toArray(). Converts ArrayList to standard array format.
ArrayList.toArray(). Converts ArrayList to standard array format.
← Didn't Know|Knew It →
Identify the method to check if an ArrayList is empty.
Identify the method to check if an ArrayList is empty.
Tap to reveal answer
ArrayList.isEmpty(). Returns boolean indicating if size equals zero.
ArrayList.isEmpty(). Returns boolean indicating if size equals zero.
← Didn't Know|Knew It →
What is the time complexity of removing an element from the beginning of an ArrayList?
What is the time complexity of removing an element from the beginning of an ArrayList?
Tap to reveal answer
O(n). Requires shifting all subsequent elements leftward.
O(n). Requires shifting all subsequent elements leftward.
← Didn't Know|Knew It →
Find the syntax to declare an ArrayList of strings with initial capacity.
Find the syntax to declare an ArrayList of strings with initial capacity.
Tap to reveal answer
ArrayList list = new ArrayList<>(capacity);. Pre-allocates memory for specified number of elements.
ArrayList
← Didn't Know|Knew It →
What is the purpose of traversing an ArrayList using an enhanced for loop?
What is the purpose of traversing an ArrayList using an enhanced for loop?
Tap to reveal answer
To simplify iteration over elements. Enhanced for loop provides cleaner, safer iteration.
To simplify iteration over elements. Enhanced for loop provides cleaner, safer iteration.
← Didn't Know|Knew It →
Identify the method to add all elements from one ArrayList to another.
Identify the method to add all elements from one ArrayList to another.
Tap to reveal answer
ArrayList.addAll(collection). Appends all elements from source collection.
ArrayList.addAll(collection). Appends all elements from source collection.
← Didn't Know|Knew It →
State the method to find the last index of an element in an ArrayList.
State the method to find the last index of an element in an ArrayList.
Tap to reveal answer
ArrayList.lastIndexOf(element). Returns highest index where element appears.
ArrayList.lastIndexOf(element). Returns highest index where element appears.
← Didn't Know|Knew It →
What loop structure uses an iterator to traverse an ArrayList?
What loop structure uses an iterator to traverse an ArrayList?
Tap to reveal answer
Iterator loop. Uses Iterator object for controlled sequential access.
Iterator loop. Uses Iterator object for controlled sequential access.
← Didn't Know|Knew It →
Which method replaces an element at a specific index in an ArrayList?
Which method replaces an element at a specific index in an ArrayList?
Tap to reveal answer
ArrayList.set(index, element). Updates element at specified index position.
ArrayList.set(index, element). Updates element at specified index position.
← Didn't Know|Knew It →
Which method retrieves an element from an ArrayList at a specific index?
Which method retrieves an element from an ArrayList at a specific index?
Tap to reveal answer
ArrayList.get(index). Returns element at specified position without removing it.
ArrayList.get(index). Returns element at specified position without removing it.
← Didn't Know|Knew It →
State the method to clear all elements from an ArrayList.
State the method to clear all elements from an ArrayList.
Tap to reveal answer
ArrayList.clear(). Removes all elements, making the ArrayList empty.
ArrayList.clear(). Removes all elements, making the ArrayList empty.
← Didn't Know|Knew It →
Write the syntax to traverse an ArrayList using an iterator.
Write the syntax to traverse an ArrayList using an iterator.
Tap to reveal answer
while (iterator.hasNext()) { iterator.next(); }. Uses iterator methods to safely traverse elements.
while (iterator.hasNext()) { iterator.next(); }. Uses iterator methods to safely traverse elements.
← Didn't Know|Knew It →