Java Reference
In-Depth Information
First
remove
ensures that the array references will stay in bounds. You
could handle the actual exception later, but checking now gives you
more control. Then
remove
calculates how many characters follow the re-
moved portion. If there are none, it truncates and returns. Otherwise,
remove
retrieves them using
getChars
and then truncates the buffer and
appends the leftover characters before returning.
13.4.3. Capacity Management
The buffer of a
StringBuilder
object has a capacity, which is the length
of the string it can store before it must allocate more space. The buffer
grows automatically as characters are added, but it is more efficient to
specify the size of the buffer only once.
You set the initial size of a
StringBuilder
object by using the constructor
that takes a single
int
:
public
StringBuilder(int capacity)
Constructs a
StringBuilder
with the given initial
capacity
and
an initial value of
""
.
public void
ensureCapacity(int minimum)
Ensures that the capacity of the buffer is at least the specified
minimum
.
public int
capacity()
Returns the current capacity of the buffer.
public void
trimToSize()