ArrayList Traversals - AP Computer Science A
- Identify the index of the first element in an ArrayList.
- 0. ArrayLists use zero-based indexing like arrays.
- State the method to sort elements in an ArrayList using natural ordering.
- Collections.sort(arrayList). Sorts elements using their natural comparison order.
- What loop structure uses an iterator to traverse an ArrayList?
- Iterator loop. Uses Iterator object for controlled sequential access.
- Identify a method to get the size of an ArrayList.
- ArrayList.size(). Returns number of elements currently stored in the list.
- What is the return type of ArrayList.size()?
- int. Returns integer count of elements in the ArrayList.
- 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.
- 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.
- What exception is thrown when accessing an invalid index in an ArrayList?
- IndexOutOfBoundsException. Runtime exception for accessing non-existent index positions.
- Identify the method to add an element to the end of an ArrayList.
- ArrayList.add(element). Appends element to the end of the list.
- What is the purpose of traversing an ArrayList?
- To access each element in the ArrayList. Allows examination or processing of every element sequentially.
- Find the syntax to initialize an empty ArrayList of integers.
- ArrayList<Integer> list = new ArrayList<>();. Creates new empty ArrayList with Integer type parameter.
- Identify the keyword to break out of a loop early.
- break. Immediately exits loop when condition is met.
- Which method retrieves an element from an ArrayList at a specific index?
- ArrayList.get(index). Returns element at specified position without removing it.
- Write the syntax to traverse an ArrayList using an iterator.
- while (iterator.hasNext()) { iterator.next(); }. Uses iterator methods to safely traverse elements.
- Identify the method to add all elements from one ArrayList to another.
- ArrayList.addAll(collection). Appends all elements from source collection.
- Choose the method to replace all elements in an ArrayList with specified elements.
- ArrayList.replaceAll(unaryOperator). Applies function to transform each element in place.
- Which method replaces an element at a specific index in an ArrayList?
- ArrayList.set(index, element). Updates element at specified index position.
- What is the time complexity of removing an element from the beginning of an ArrayList?
- O(n). Requires shifting all subsequent elements leftward.
- Check if an ArrayList contains a specific element.
- ArrayList.contains(element). Returns boolean indicating element presence in the list.
- What is the time complexity of accessing an element by index in an ArrayList?
- O(1). Direct array access provides constant time retrieval.
- 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.
- What is the time complexity of adding an element at the end of an ArrayList?
- O(1). Appending to end requires no element shifting.
- What method of Iterator checks for more elements?
- Iterator.hasNext(). Returns true if more elements remain to iterate.
- State the method to find the last index of an element in an ArrayList.
- ArrayList.lastIndexOf(element). Returns highest index where element appears.
- 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.
- Identify the method to obtain an iterator for an ArrayList.
- ArrayList.iterator(). Returns Iterator object for traversing the ArrayList.
- Identify the method to check if an ArrayList is empty.
- ArrayList.isEmpty(). Returns boolean indicating if size equals zero.
- Identify the method to remove an element by index from an ArrayList.
- ArrayList.remove(index). Removes element at specified position and returns it.
- What is a common error when accessing elements during an ArrayList traversal?
- ConcurrentModificationException. Thrown when modifying ArrayList during iteration.
- What type of loop is commonly used to traverse an ArrayList?
- For-each loop. Enhanced for loop simplifies iteration without index management.
- What is the return type of ArrayList.get(index)?
- Object or specified type. Returns the generic type or Object if unspecified.
- State the method to remove the first occurrence of an element from an ArrayList.
- ArrayList.remove(element). Searches and removes first matching element found.
- What method checks if two ArrayLists contain the same elements in the same order?
- ArrayList.equals(object). Compares both content and order of elements.
- 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.
- Find the index of the last element in an ArrayList of size .
- . Last index equals size minus one due to zero-based indexing.
- Choose the method to convert an ArrayList to an array.
- ArrayList.toArray(). Converts ArrayList to standard array format.
- 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.
- State the method to clear all elements from an ArrayList.
- ArrayList.clear(). Removes all elements, making the ArrayList empty.
- 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.