Junior — Middle
How is the time complexity of searching for an element by index in an ArrayList determined?
sobes.tech AI
Answer from AI
In ArrayList in Java, the operation of searching for an element by index is performed in constant time — O(1). This is because ArrayList is implemented based on an array, and access to an element by index occurs directly through its memory address, without traversing other elements.
Example:
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
String element = list.get(1); // Accessing element at index 1 — O(1)