1 Answers
📚 Topic Summary
The ArrayList in Java is a fundamental data structure in AP Computer Science A, offering a dynamic, resizable array implementation. Unlike traditional arrays with fixed sizes, an ArrayList can grow or shrink as needed, making it incredibly flexible for managing collections of objects.
The primary method for adding elements to an ArrayList is the add() method, which comes in two main overloads: add(E e) and add(int index, E e). The add(E e) method is straightforward; it appends the specified element e to the very end of the list. On the other hand, add(int index, E e) is more nuanced. It inserts the element e at a specific index, causing all subsequent elements (those at or after the specified index) to shift one position to the right, increasing their indices by one. Understanding these behaviors is crucial for predicting how your list will change and for avoiding common errors like IndexOutOfBoundsException.
📝 Part A: Vocabulary
Match each term with its correct definition by writing the letter of the definition next to the term.
- 💡 Terms:
- 1. ArrayList
- 2.
add(E e) - 3. Index
- 4.
add(int index, E e) - 5. Shifting
- 📚 Definitions: (Definitions are mixed up)
- A. The zero-based position of an element within a list.
- B. Appends an element to the end of the list.
- C. A dynamic, resizable array implementation in Java, part of the Collections Framework.
- D. The process where elements move to accommodate a new insertion or removal.
- E. Inserts an element at a specific position in the list, shifting existing elements to the right.
✍️ Part B: Fill in the Blanks
Complete the following paragraph by filling in the blanks with the most appropriate terms.
The ArrayList in Java is a ________________ data structure that can dynamically resize. The add() method has two common overloads. The add(E e) method appends an element to the ________________ of the list. When using add(int index, E e), the element is inserted at a specified ________________, and all subsequent elements are ________________ to the right. This means their indices ________________ by one. Attempting to add an element at an invalid index (e.g., negative or greater than the current size) will result in an ________________ exception.
🧠 Part C: Critical Thinking
Consider an ArrayList<String> initialized as ["apple", "banana", "cherry"]. Describe the exact state of the ArrayList after the following sequence of operations:
- 🍎 1.
list.add("date"); - 🍌 2.
list.add(1, "grape"); - 🍒 3.
list.add(0, "fig");
Explain your reasoning for each step, focusing on how elements are shifted and the final state of the list.
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀