Sobes.tech
Junior — Middle

What is the time complexity of retrieving an element from an ArrayList by index?

sobes.tech AI

Answer from AI

In Java, the operation of retrieving an element from an ArrayList by index has a time complexity of O(1), meaning it is performed in constant time. This is because ArrayList is implemented based on an array, and access to an element by index occurs directly through its memory address.

Example:

ArrayList<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
String fruit = list.get(1); // Retrieving the "banana" element in O(1)
What is the time complexity of retrieving an element… - sobes.tech