[英]Time complexity of StringBuilder length() method
What is the time complexity of length() method from StringBuilder class? StringBuilder class 中 length() 方法的时间复杂度是多少?
String str = "hello";
StringBuilder sb = new StringBuilder(str);
System.out.println(sb.length()); // should print 5
My assumption is O(n) where n is number of characters in the string, similar to length() from String class.我的假设是 O(n),其中 n 是字符串中的字符数,类似于字符串 class 中的 length()。
This is a member variable in StringBuilder
class:这是StringBuilder
中的一个成员变量class:
/**
* The count is the number of characters used.
*/
int count;
And this is the code of length()
:这是length()
的代码:
/**
* Returns the length (character count).
*
* @return the length of the sequence of characters currently
* represented by this object
*/
@Override
public int length() {
return count;
}
What do you think?你怎么看?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.