cin*_*ast 6 java string runtime copy system
String.toCharArray()java中的运行时间是多少?源代码是
public char[] toCharArray() {
// Cannot use Arrays.copyOf because of class initialization order issues
char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);
return result;
}
Run Code Online (Sandbox Code Playgroud)
难道System.arrayCopy?有 O(n) 的运行时间吗?源代码并没有真正说明它是如何实现的。它是否遍历每个元素并复制它?谢谢。
System.arraycopy()通常是内在的并且非常快。也就是说,它仍然需要查看(并复制)数组的每个元素,因此它的渐近复杂度在数组的长度上是线性的,即O(n)。
因此 的复杂度toCharArray()是O(n),其中n是字符串的长度。