<分区>
Possible Duplicate:
In Java, for a string x, what is the runtime cost of s.length()? Is it O(1) or O(n)?
String 类的 length 方法在 Java 中究竟是如何工作的?
它是线性时间的 for 循环还是有一个字段跟踪长度以使其时间恒定?
<分区>
Possible Duplicate:
In Java, for a string x, what is the runtime cost of s.length()? Is it O(1) or O(n)?
String 类的 length 方法在 Java 中究竟是如何工作的?
它是线性时间的 for 循环还是有一个字段跟踪长度以使其时间恒定?
最佳答案
String
的长度被保存为一个字段,如下所示:http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java#String.length%28%29
关于Java字符串长度方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11491665/