Search code examples
javaarraylisttime-complexitysublistconstant-time

What is the time complexity of Java's ArrayList.sublist(startIndex, endIndex) method?


The question basically says it all. Suppose I have a (sorted) list that can contain anywhere from 1K to 1M items. I have a starting index and an ending index. If I use the ArrayList.sublist(start, end) method, is the time complexity O(n) or O(1)? I did check for answers here since I'd think would be a common question, but although I found a duplicate answer for a LinkedList, I couldn't find a specific question about ArrayList. Thanks to all for their answers!


Solution

  • The sub-list is backed by the source list. There is no copy step, so the time complexity is O(1).