StringBuilder容量()
我注意到 capacity
方法返回 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我将尝试用一些例子来解释这一点。
length()
- 构建器中字符序列的长度由于此字符串生成器不包含任何内容,因此其长度将为 0。
capacity()
- 已分配的字符空间数。当您尝试构造具有空内容的字符串构建器时,默认情况下它会将初始化大小设置为长度+16,即0+16。因此这里的容量将返回 16。
注意:capacity() 方法返回的容量始终大于或等于长度(通常大于),并且会根据需要自动扩展以适应字符串生成器的添加。
容量函数背后的逻辑:
此分析取自 实际的 StringBuilder.java 代码
I will try to explain this with some example.
length()
- the length of the character sequence in the buildersince this stringbuilder doesn't contain any content, its length will be 0.
capacity()
- the number of character spaces that have been allocated.When you try to construct a stringbuilder with empty content, by default it takes the initialize size as length+16 which is 0+16. so capacity would return 16 here.
Note: The capacity, which is returned by the capacity() method, is always greater than or equal to the length (usually greater than) and will automatically expand as necessary to accommodate additions to the string builder.
The logic behind the capacity function:
This analysis is take from actual StringBuilder.java code
当您追加到
StringBuilder
时,会发生以下逻辑:其中
newCount
是所需的字符数,value.length
是当前大小缓冲区。expandCapacity
只是增加支持char[]
的大小ensureCapacity()
方法是调用expandCapacity()< /code>,其文档说:
When you append to the
StringBuilder
, the following logic happens:where
newCount
is the number of characters needed, andvalue.length
is the current size of the buffer.expandCapacity
simply increases the size of the backingchar[]
The
ensureCapacity()
method is the public way to callexpandCapacity()
, and its docs say:此函数执行的操作与您的预期不同 - 它为您提供了此 StringBuilder 实例内存此时可以容纳的最大字符数。
字符串生成器必读
This function does something different than you expect - it gives you the max number of chars this StringBuilder instance memory can hold at this time.
String Builder must read
逻辑如下:
如果您定义一个没有构造函数的
StringBuilder
类的新实例,例如new StringBuilder();
,则默认容量为 16。构造函数可以是
int
或String
。对于
String
构造函数,默认容量的计算方式如下对于
int
构造函数,容量的计算方式如下如果附加一个新的
String
到StringBuilder
并且String
的新长度大于当前容量,则容量计算如下:Here's the logic:
If you define a new instance of the
StringBuilder
class without a constructor, like sonew StringBuilder();
the default capacity is 16.A constructor can be either an
int
or aString
.For a
String
constructor, the default capacity is calculated like thisFor an
int
constructor, the capacity is calculated like thisIf a new
String
is appended to theStringBuilder
and the new length of theString
is greater than the current capacity, then the capacity is calculated like this:编辑:抱歉 - 以下是有关 .NET StringBuilder 的信息,与原始问题并不严格相关。
http://johnnycoder.com/blog/2009/01 /05/stringbuilder-required-capacity-algorithm/
StringBuilder 为您可能添加到其中的子字符串分配空间(很像 List 为其包装的数组创建空间)。如果您想要字符串的实际长度,请使用 StringBuilder.Length。
EDIT: Apologies - the below is information on .NET's StringBuilder, and is not strictly relevant to the original question.
http://johnnycoder.com/blog/2009/01/05/stringbuilder-required-capacity-algorithm/
StringBuilder allocates space for substrings you might add to it (much like List creates space the array it wraps). If you want the actual length of the string, use StringBuilder.Length.
从API:
每当您追加内容时,都会进行检查以确保更新的 StringBuilder 不会超出其容量,如果超出,则会调整 StringBuilder 的内部存储大小:
当添加的数据超过其容量时,它将重新调整- 根据以下公式确定大小:
有关详细信息,请参阅 JDK 附带的 src.zip 文件。 (以上摘自 1.6 JDK 的片段)
From the API:
Whenever you append something, there is a check to make sure that the updated StringBuilder won't exceed its capacity, and if it does, the internal storage of the StringBuilder is resized:
When data is added to it that exceeds its capacity it is re-sized according to the following formula:
See the
src.zip
file that comes with the JDK for more information. (Above snippets taken from the 1.6 JDK)你可以进入JDK代码看看它是如何工作的,它基于一个char数组:
new char[capacity]
,它类似于ArrayList
的工作原理( 何时使用 LinkedList 而不是 ArrayList?)。两者都使用数组来提高“硬件效率”,技巧是分配一大块内存并在其中工作,直到内存耗尽并需要下一个大块来继续(扩展/增长)。You can go inside the JDK code and see how it works, it is based on a char array:
new char[capacity]
, it is similar to how theArrayList
works (When to use LinkedList over ArrayList?). Both use arrays to be 'hardware efficient', the trick is to allocate a large chunk of memory and work in it until you run out of memory and need the next big chunk to continue (expand/grow).以 Java 1.8
为例:
in Java 1.8
for example :