Junior — Middle
How is the speed of obtaining an element by index in ArrayList determined?
sobes.tech AI
Answer from AI
In ArrayList, access speed to an element by index is considered constant — O(1). This is because ArrayList is implemented based on an array, where elements are stored in a contiguous block of memory.
When calling the get(index) method, a simple calculation of the element's address occurs using the formula: base address + index * size_of_element, after which a reference to the object is returned.
Therefore, index access is very fast and does not depend on the size of the list, unlike, for example, LinkedList, where accessing by index requires traversing the elements sequentially.