Why use `get()` instead of bracket notation with ArrayLists?
ArrayLists are objects, not primitive arrays. `get()` is the method to access elements.
Why does removing elements during traversal require special care?
Removing shifts elements, potentially skipping the next element if the index isn't adjusted.
Why does adding elements during traversal require special care?
Adding elements shifts elements, potentially causing infinite loops if the index isn't adjusted correctly.
Why can't you modify an ArrayList's size using an enhanced for loop?
Enhanced for loops rely on iterators, which don't allow structural modifications during iteration.
Why is `i--` used after removing an element?
To account for the shift in elements after removal, ensuring the next element is checked.
Why is `i++` used after adding an element?
To account for the shift in elements after insertion, ensuring the next element is checked.
How does the `size()` method behave when adding or removing elements during traversal?
The `size()` method dynamically updates to reflect the current number of elements in the ArrayList.
What happens if you try to access an index outside the range of an ArrayList?
An `IndexOutOfBoundsException` is thrown.
What is the primary difference between traversing an array and an ArrayList using a for loop?
Arrays use bracket notation and the `length` property, while ArrayLists use the `get()` and `size()` methods.
In what scenario is it appropriate to use a regular for loop instead of an enhanced for loop when traversing an ArrayList?
When you need to modify the ArrayList's structure (add or remove elements) during traversal.
What is an ArrayList?
A resizable array implementation of the List interface.
What is IndexOutOfBoundsException?
An exception thrown when trying to access an invalid index in an ArrayList.
What is ConcurrentModificationException?
An exception thrown when an ArrayList is structurally modified while being traversed with an enhanced for loop.
What does 'pass-by-value' mean?
A method of passing arguments to a function where a copy of the variable's value is passed, not the original variable itself.
What is the purpose of the `get()` method in ArrayList?
It retrieves the element at a specific index in the ArrayList.
What is the purpose of the `size()` method in ArrayList?
It returns the number of elements currently in the ArrayList.
What is the purpose of the `remove(i)` method in ArrayList?
It removes the element at the specified index `i` from the ArrayList.
What is the purpose of the `add(i, element)` method in ArrayList?
It inserts the specified element at the specified position `i` in the ArrayList.
What is traversing?
Visiting each element in a data structure, such as an ArrayList, in a specific order.
What is the purpose of the `set()` method in ArrayList?
Replaces the element at the specified position in this list with the specified element.
What are the differences between using a regular for loop and an enhanced for loop to traverse an ArrayList?
Regular for loop: Requires manual index management, allows modifications to the ArrayList during traversal. Enhanced for loop: Simpler syntax, does not allow modifications to the ArrayList during traversal.
What are the differences between modifying an ArrayList using `set()` within a for loop vs. modifying the enhanced for loop variable?
`set()`: Changes the actual element in the ArrayList. Enhanced for loop variable modification: Only changes the loop variable, not the ArrayList element (pass-by-value).
What are the differences between handling `IndexOutOfBoundsException` and `ConcurrentModificationException`?
IndexOutOfBoundsException: Occurs due to invalid index access. ConcurrentModificationException: Occurs due to structural modification during enhanced for loop traversal.
Compare the performance implications of adding elements at the end versus the beginning of an ArrayList.
Adding at the end: Generally faster, O(1) amortized time. Adding at the beginning: Slower, O(n) time due to element shifting.
Compare the use of `removeIf()` with a regular for loop for removing elements from an ArrayList based on a condition.
`removeIf()`: More concise, uses lambda expressions. Regular for loop: More control over the removal process and index management.
What are the differences between `ArrayList` and a standard Java array when traversing and modifying elements?
ArrayList: Dynamic size, uses `get()` and `set()` methods. Array: Fixed size, uses bracket notation for access and modification.
Compare the efficiency of searching for an element in an ArrayList using a loop versus using the `contains()` method.
Loop: Requires manual iteration, O(n) time complexity. `contains()`: Uses built-in optimized search, potentially faster in some implementations.
What are the differences between using a `while` loop and a `for` loop to traverse an ArrayList?
While loop: Requires manual initialization and increment of the index. For loop: More concise and manages index automatically.
Compare the readability and maintainability of using streams and lambda expressions versus traditional loops for processing ArrayList elements.
Streams and lambdas: More concise and declarative, can improve readability. Traditional loops: More verbose, but can be easier to understand for complex logic.
What are the differences between using `Iterator` and enhanced for loop for traversing an ArrayList?
`Iterator`: Allows removing elements during traversal using `iterator.remove()`. Enhanced for loop: More concise but does not allow removing elements.