Junior — Middle
What provides access to an element by index in an ArrayList in constant time?
sobes.tech AI
Answer from AI
Access to an element by index in ArrayList is provided in constant time (O(1)) due to the internal structure of ArrayList — it is implemented based on an array.
An array allows direct access to an element by index because elements are stored in a contiguous memory area, and the address calculation for an element is done by the formula:
start_address_of_array + index * size_of_element
Therefore, the get(index) operation in ArrayList is very fast and does not depend on the size of the list.