ArrayList in AP Computer Science A
Which statement correctly checks if an ArrayList called numbers
contains no elements?
numbers.length() == 0
numbers.isEmpty()
numbers.hasNoElements()
numbers.size() == -1
Given an array list "values" filled with integers, which loop ensures checking each element exactly once starting from first element until last?
For (int i=0; i < values.size(); i++) {}
Do-while (values.iterator().hasNext()) {}
For (int i=values.size()-1; i >=0 ; i--) {}
While (!values.isEmpty()) {}
When iterating through an ArrayList in Java using a standard loop structure, what must be ensured about the loop condition?
The condition properly uses the size() method as part of its check so as not looping beyond the list's bounds.
Relies exclusively on hard-coded numeric values instead of relying on dynamic aspects like the size of the ArrayList being iterated over.
Ensures each element is visited exactly twice to ensure thorough processing of elements within.
Only increments or skips odd indexed elements during traversal to preserve memory efficiency and application performance.
How should we avoid an infinite loop when adding elements to an ArrayList during traversal with an indexed for loop?
Set the loop variable to zero inside the loop body before the loop incrementation
Decrement the loop variable inside the loop body before the loop incrementation
Increment the loop variable inside the loop body before the loop incrementation
We can’t avoid an infinite loop using a regular for loop and must use an enhanced for loop instead
Which ArrayList methods do we use in the for loop implementation of traversing an ArrayList?
get() and length()
size() and length()
get() and size()
set() and size()
If we delete an element at index i during traversal of an ArrayList, what must we do before inside the loop body, before the loop incrementation, to avoid skipping an element?
i = 0
i = 1
i--
i++
In what scenario might a programmer prefer using methods specifically designed for ArrayLists over those available through the List interface when dealing with data manipulation operations?
A programmer might prefer methods specifically designed for ArrayLists over those available through the List interface when efficiency is a concern.
Ultimately, ArrayList has more efficient implementations for certain operations than methods defined in the generic List interface.
Correct answer - explains why ArrayList is referred to as providing specific method implementations, while the List interface represents the general case that may not be optimized for a particular use case.

How are we doing?
Give us your feedback and let us know how we can improve
What would you use to remove the element at index 'i' from an ArrayList called 'list'?
eliminate(i)
remove(i)
replace(i)
discard(i)
What is an efficient way to compute the sum of all elements in an ArrayList
Utilize a regular for loop with a cumulative variable that adds each individual element.
Use an enhanced for loop to sum values since it provides no access to the list index.
Utilize iterator objects methods to calculate the sum by passing summation commands in each call.
Utilize a recursive method to pass through the list and cumulatively add up values.
How could you avoid a ConcurrentModificationException when attempting to remove selected elements from an ArrayList during iteration?
Use enhanced for loop and remove elements directly.
Iterate over the ArrayList with a regular for loop and call remove(index) immediately.
Apply a foreach loop to remove items based on match conditions.
Utilize iterator removal method after checking with next().