AP Computer Science a Flashcards: Arraylist Traversals

Study Arraylist Traversals in AP Computer Science a with focused flashcards that help you recognize the idea, recall the key rule, and apply it in practice-style prompts.

AP Computer Science a

Arraylist Traversals

0 mastered0 still learning

0% Complete

QUESTION
1/ 78

Find the syntax to initialize an empty ArrayList of integers.

Tap card or press Space to flip

ANSWER

ArrayList list = new ArrayList<>();. Creates new empty ArrayList with Integer type parameter.

How well did you know it?

Card 1 / 78

What this deck covers

This deck focuses on Arraylist Traversals, giving you a quick way to review the definitions, rules, and examples that matter most for AP Computer Science a.

How to use these flashcards

Work through these flashcards in short sessions. Try to answer each prompt before flipping the card, then revisit any cards you miss until the explanation feels automatic.

All flashcards

Flashcard 1: Find the syntax to initialize an empty ArrayList of integers.

Answer: ArrayList list = new ArrayList<>();. Creates new empty ArrayList with Integer type parameter.

Flashcard 2: Identify the index of the first element in an ArrayList.

Answer:

  1. ArrayLists use zero-based indexing like arrays.

Flashcard 3: State the method to sort elements in an ArrayList using natural ordering.

Answer: Collections.sort(arrayList). Sorts elements using their natural comparison order.

Flashcard 4: What loop structure uses an iterator to traverse an ArrayList?

Answer: Iterator loop. Uses Iterator object for controlled sequential access.

Flashcard 5: What loop structure uses an iterator to traverse an ArrayList?

Answer: Iterator loop. Uses Iterator object for controlled sequential access.

Flashcard 6: Identify a method to get the size of an ArrayList.

Answer: ArrayList.size(). Returns number of elements currently stored in the list.

Flashcard 7: What is the return type of ArrayList.size()?

Answer: int. Returns integer count of elements in the ArrayList.

Flashcard 8: Write the for-each loop syntax to traverse an ArrayList of integers.

Answer: for (Integer num : arrayList) { }. Enhanced for loop iterates through each Integer in the list.

Flashcard 9: Choose the method to convert an ArrayList to an array.

Answer: ArrayList.toArray(). Converts ArrayList to standard array format.

Flashcard 10: Identify the method to obtain an iterator for an ArrayList.

Answer: ArrayList.iterator(). Returns Iterator object for traversing the ArrayList.

Flashcard 11: Identify the method to retain only elements in an ArrayList that are contained in another collection.

Answer: ArrayList.retainAll(collection). Keeps only elements present in both collections.

Flashcard 12: Write the for-each loop syntax to traverse an ArrayList of integers.

Answer: for (Integer num : arrayList) { }. Enhanced for loop iterates through each Integer in the list.

Flashcard 13: What exception is thrown when accessing an invalid index in an ArrayList?

Answer: IndexOutOfBoundsException. Runtime exception for accessing non-existent index positions.

Flashcard 14: What is the return type of ArrayList.size()?

Answer: int. Returns integer count of elements in the ArrayList.

Flashcard 15: Identify the method to add an element to the end of an ArrayList.

Answer: ArrayList.add(element). Appends element to the end of the list.

Flashcard 16: What is the time complexity of removing an element from the beginning of an ArrayList?

Answer: O(n). Requires shifting all subsequent elements leftward.

Flashcard 17: State the method to clear all elements from an ArrayList.

Answer: ArrayList.clear(). Removes all elements, making the ArrayList empty.

Flashcard 18: What type of loop is commonly used to traverse an ArrayList?

Answer: For-each loop. Enhanced for loop simplifies iteration without index management.

Flashcard 19: Choose the method to add an element at a specific index in an ArrayList.

Answer: ArrayList.add(index, element). Inserts element at specified position, shifting existing elements.

Flashcard 20: Choose the method to replace all elements in an ArrayList with specified elements.

Answer: ArrayList.replaceAll(unaryOperator). Applies function to transform each element in place.

Flashcard 21: What is the purpose of traversing an ArrayList?

Answer: To access each element in the ArrayList. Allows examination or processing of every element sequentially.

Flashcard 22: Given an ArrayList of strings, write a loop to print each element.

Answer: for (String s : arrayList) { System.out.println(s); }. Enhanced for loop prints each string element sequentially.

Flashcard 23: State the method to remove the first occurrence of an element from an ArrayList.

Answer: ArrayList.remove(element). Searches and removes first matching element found.

Flashcard 24: Find the syntax to initialize an empty ArrayList of integers.

Answer: ArrayList list = new ArrayList<>();. Creates new empty ArrayList with Integer type parameter.

Flashcard 25: Identify the keyword to break out of a loop early.

Answer: break. Immediately exits loop when condition is met.

Flashcard 26: Identify a method to get the size of an ArrayList.

Answer: ArrayList.size(). Returns number of elements currently stored in the list.

Flashcard 27: Find the index of the last element in an ArrayList of size nn.

Answer: n1n - 1. Last index equals size minus one due to zero-based indexing.

Flashcard 28: What is a common error when accessing elements during an ArrayList traversal?

Answer: ConcurrentModificationException. Thrown when modifying ArrayList during iteration.

Flashcard 29: Which method retrieves an element from an ArrayList at a specific index?

Answer: ArrayList.get(index). Returns element at specified position without removing it.

Flashcard 30: Write the syntax to traverse an ArrayList using an iterator.

Answer: while (iterator.hasNext()) { iterator.next(); }. Uses iterator methods to safely traverse elements.

Flashcard 31: State the method to sort elements in an ArrayList using natural ordering.

Answer: Collections.sort(arrayList). Sorts elements using their natural comparison order.

Flashcard 32: Identify the method to add all elements from one ArrayList to another.

Answer: ArrayList.addAll(collection). Appends all elements from source collection.

Flashcard 33: State the method to find the last index of an element in an ArrayList.

Answer: ArrayList.lastIndexOf(element). Returns highest index where element appears.

Flashcard 34: Which method retrieves an element from an ArrayList at a specific index?

Answer: ArrayList.get(index). Returns element at specified position without removing it.

Flashcard 35: Choose the method to replace all elements in an ArrayList with specified elements.

Answer: ArrayList.replaceAll(unaryOperator). Applies function to transform each element in place.

Flashcard 36: Which method replaces an element at a specific index in an ArrayList?

Answer: ArrayList.set(index, element). Updates element at specified index position.

Flashcard 37: Identify the keyword to break out of a loop early.

Answer: break. Immediately exits loop when condition is met.

Flashcard 38: What is the time complexity of removing an element from the beginning of an ArrayList?

Answer: O(n). Requires shifting all subsequent elements leftward.

Flashcard 39: Check if an ArrayList contains a specific element.

Answer: ArrayList.contains(element). Returns boolean indicating element presence in the list.

Flashcard 40: Identify the index of the first element in an ArrayList.

Answer:

  1. ArrayLists use zero-based indexing like arrays.

Flashcard 41: What is the purpose of traversing an ArrayList?

Answer: To access each element in the ArrayList. Allows examination or processing of every element sequentially.

Flashcard 42: What method of Iterator checks for more elements?

Answer: Iterator.hasNext(). Returns true if more elements remain to iterate.

Flashcard 43: What is the time complexity of accessing an element by index in an ArrayList?

Answer: O(1). Direct array access provides constant time retrieval.

Flashcard 44: What is the time complexity of accessing an element by index in an ArrayList?

Answer: O(1). Direct array access provides constant time retrieval.

Flashcard 45: Identify the method to add an element to the end of an ArrayList.

Answer: ArrayList.add(element). Appends element to the end of the list.

Flashcard 46: What is the return type of ArrayList.get(index)?

Answer: Object or specified type. Returns the generic type or Object if unspecified.

Flashcard 47: Find the syntax to declare an ArrayList of strings with initial capacity.

Answer: ArrayList list = new ArrayList<>(capacity);. Pre-allocates memory for specified number of elements.

Flashcard 48: What is the time complexity of adding an element at the end of an ArrayList?

Answer: O(1). Appending to end requires no element shifting.

Flashcard 49: What method checks if two ArrayLists contain the same elements in the same order?

Answer: ArrayList.equals(object). Compares both content and order of elements.

Flashcard 50: What is the time complexity of adding an element at the end of an ArrayList?

Answer: O(1). Appending to end requires no element shifting.

Flashcard 51: What exception is thrown when accessing an invalid index in an ArrayList?

Answer: IndexOutOfBoundsException. Runtime exception for accessing non-existent index positions.

Flashcard 52: What method of Iterator checks for more elements?

Answer: Iterator.hasNext(). Returns true if more elements remain to iterate.

Flashcard 53: State the method to find the last index of an element in an ArrayList.

Answer: ArrayList.lastIndexOf(element). Returns highest index where element appears.

Flashcard 54: Given an ArrayList of strings, write a loop to print each element.

Answer: for (String s : arrayList) { System.out.println(s); }. Enhanced for loop prints each string element sequentially.

Flashcard 55: Identify the method to obtain an iterator for an ArrayList.

Answer: ArrayList.iterator(). Returns Iterator object for traversing the ArrayList.

Flashcard 56: Identify the method to check if an ArrayList is empty.

Answer: ArrayList.isEmpty(). Returns boolean indicating if size equals zero.

Flashcard 57: Identify the method to remove an element by index from an ArrayList.

Answer: ArrayList.remove(index). Removes element at specified position and returns it.

Flashcard 58: What is a common error when accessing elements during an ArrayList traversal?

Answer: ConcurrentModificationException. Thrown when modifying ArrayList during iteration.

Flashcard 59: What type of loop is commonly used to traverse an ArrayList?

Answer: For-each loop. Enhanced for loop simplifies iteration without index management.

Flashcard 60: What is the return type of ArrayList.get(index)?

Answer: Object or specified type. Returns the generic type or Object if unspecified.

Flashcard 61: State the method to remove the first occurrence of an element from an ArrayList.

Answer: ArrayList.remove(element). Searches and removes first matching element found.

Flashcard 62: What is the time complexity of iterating over an ArrayList using a for-each loop?

Answer: O(n). Must visit each element once for complete traversal.

Flashcard 63: Identify the method to remove an element by index from an ArrayList.

Answer: ArrayList.remove(index). Removes element at specified position and returns it.

Flashcard 64: What method checks if two ArrayLists contain the same elements in the same order?

Answer: ArrayList.equals(object). Compares both content and order of elements.

Flashcard 65: Which method replaces an element at a specific index in an ArrayList?

Answer: ArrayList.set(index, element). Updates element at specified index position.

Flashcard 66: Write the syntax to traverse an ArrayList using an iterator.

Answer: while (iterator.hasNext()) { iterator.next(); }. Uses iterator methods to safely traverse elements.

Flashcard 67: What is the purpose of traversing an ArrayList using an enhanced for loop?

Answer: To simplify iteration over elements. Enhanced for loop provides cleaner, safer iteration.

Flashcard 68: Check if an ArrayList contains a specific element.

Answer: ArrayList.contains(element). Returns boolean indicating element presence in the list.

Flashcard 69: What is the purpose of traversing an ArrayList using an enhanced for loop?

Answer: To simplify iteration over elements. Enhanced for loop provides cleaner, safer iteration.

Flashcard 70: Find the index of the last element in an ArrayList of size nn.

Answer: n1n - 1. Last index equals size minus one due to zero-based indexing.

Flashcard 71: Find the syntax to declare an ArrayList of strings with initial capacity.

Answer: ArrayList list = new ArrayList<>(capacity);. Pre-allocates memory for specified number of elements.

Flashcard 72: Choose the method to convert an ArrayList to an array.

Answer: ArrayList.toArray(). Converts ArrayList to standard array format.

Flashcard 73: What is the time complexity of iterating over an ArrayList using a for-each loop?

Answer: O(n). Must visit each element once for complete traversal.

Flashcard 74: State the method to clear all elements from an ArrayList.

Answer: ArrayList.clear(). Removes all elements, making the ArrayList empty.

Flashcard 75: Identify the method to add all elements from one ArrayList to another.

Answer: ArrayList.addAll(collection). Appends all elements from source collection.

Flashcard 76: Identify the method to retain only elements in an ArrayList that are contained in another collection.

Answer: ArrayList.retainAll(collection). Keeps only elements present in both collections.

Flashcard 77: Choose the method to add an element at a specific index in an ArrayList.

Answer: ArrayList.add(index, element). Inserts element at specified position, shifting existing elements.

Flashcard 78: Identify the method to check if an ArrayList is empty.

Answer: ArrayList.isEmpty(). Returns boolean indicating if size equals zero.