StringBuilder容量() [英] StringBuilder capacity()

查看:124
本文介绍了StringBuilder容量()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到容量方法返回 StringBuilder 容量而没有逻辑
方式......有时候它的值等于字符串长度,其他时间它更大...

I noticed that the capacity method returns StringBuilder capacity without a logic way ... sometime its value is equals to the string length other time it's greater...

是否存在知道哪个是其逻辑的等式?

is there an equation for know which is its logic?

推荐答案

当你追加到 StringBuilder 时,会发生以下逻辑:

When you append to the StringBuilder, the following logic happens:

if (newCount > value.length) {
    expandCapacity(newCount);
}

其中 newCount 是所需的字符数, value.length 是缓冲区的当前大小。

where newCount is the number of characters needed, and value.length is the current size of the buffer.

expandCapacity 只需增加支持的大小 char []

expandCapacity simply increases the size of the backing char[]

ensureCapacity()方法是公共方式调用 expandCapacity(),其文档说:

The ensureCapacity() method is the public way to call expandCapacity(), and its docs say:


确保容量至少等于指定的最小值。如果当前容量小于参数,则分配具有更大容量的新内部阵列。新容量是更大的:

Ensures that the capacity is at least equal to the specified minimum. If the current capacity is less than the argument, then a new internal array is allocated with greater capacity. The new capacity is the larger of:


  • minimumCapacity参数。

  • 旧容量的两倍,加上2.

如果minimumCapacity参数是非正数,则此方法不执行任何操作,只返回。

If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.

这篇关于StringBuilder容量()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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