What is complexity of length() function in String class of Java?

后端 未结 4 1844
栀梦
栀梦 2021-02-20 07:53

Is it O(n) or O(1) (By saving length in a private variable during string allocation to the object).

if it is O(n), does it mean that the complexity of following code is

4条回答
  •  隐瞒了意图╮
    2021-02-20 08:33

    String internally maintains an array of characters and length of the array is a property of array object hence O(1) as its simple reading of property.

提交回复
热议问题