ensureCapacity(): This method is available in java.lang.AbstractStringBuilder class of Java.
Syntax:
void java.lang.AbstractStringBuilder.ensureCapacity(int minimumCapacity)
This method takes one argument of type int as its parameter. This method 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:
1. The minimumCapacity argument.
2. Twice the old capacity, plus 2.
3. If the minimumCapacity argument is nonpositive, this method takes no action and simply returns.
Parameters: One parameter is required for this method.
minimumCapacity: the minimum desired capacity.
Approach
Java
public class EnsureCapacity {public static void main(String[] args) {StringBuilder str = new StringBuilder("Hello World");int minimumCapacity = 4000;str.ensureCapacity(minimumCapacity);System.out.println(str);}}
Output:
Hello World
No comments:
Post a Comment