Sobes.tech
Middle+

What is the complexity of accessing the first and last elements in an ArrayList?

sobes.tech AI

Answer from AI

In Java, the ArrayList structure is implemented based on an array.

  • Access to the first element (index 0) is an array index operation, performed in O(1) time.
  • Access to the last element (index size-1) is also done by index, and takes O(1) time.

Thus, access to any element by index in ArrayList has a time complexity of O(1).

What is the complexity of accessing the first and… - sobes.tech