Default capacity of StringBuilder

后端 未结 8 1347
野性不改
野性不改 2020-12-16 09:42

What is the default capacity of a StringBuilder?

And when should (or shouldn\'t) the default be used?

8条回答
  •  甜味超标
    2020-12-16 10:18

    [edit: at the time, the question asked about StringList]

    Do you mean StringCollection? This uses an empty ArrayList initially, so the answer is 0. And you have no option to change it. When you first add an item, the capacity jumps to 4, then uses a doubling strategy when it fills.

    If you mean List, then it is similar (an empty T[], not an ArrayList), but you can initialize a known size if you need (i.e. you know how much data you expect). Again, the first time you Add to a List the size jumps to 4, then doubles every time it fills up.

提交回复
热议问题