String.toCharArray()的运行时间是多少? [英] What is the run time of String.toCharArray()?

查看:162
本文介绍了String.toCharArray()的运行时间是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String.toCharArray()中的运行时间是什么?源代码是

What's the run time of String.toCharArray() in java? The source code is

 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;
}

System.arrayCopy ?有运行时间O(n)?源代码并没有真正说明它是如何实现的。它是否通过每个元素并复制它?谢谢。

Does System.arrayCopy? have run time of O(n)? The source code doesn't really say much about how it's implemented. Does it go through every element and copies it? Thanks.

推荐答案

System.arraycopy()快速。也就是说,它仍然需要查看(和复制)数组的每个元素,所以其渐近复杂性在数组的长度是线性的,即 O(n)

System.arraycopy() is typically an intrinsic and is very fast. That said, it still has to look at (and copy) every element of the array, so its asymptotic complexity is linear in the length of the array, i.e. O(n).

因此 toCharArray()的复杂度为 O(n),其中 n 是字符串的长度。

Thus the complexity of toCharArray() is O(n), where n is the length of the string.

这篇关于String.toCharArray()的运行时间是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆