StringBuffer类void sureCapacity(int mincap) (StringBuffer Class void ensureCapacity(int mincap))
This method is available in package java.lang.StringBuffer.ensureCapacity(int mincap).
软件包java.lang.StringBuffer.ensureCapacity(int mincap)中提供了此方法。
This method is used to ensure the capacity is at least equal to the specified argument mincap(minimum capacity).
此方法用于确保容量至少等于指定的参数mincap(minimum Capacity) 。
If the current capacity of the StringBuffer is less than the specified argument then new space will be allocated with greater capacity.
如果StringBuffer的当前容量小于指定的参数,则将以更大的容量分配新空间。
The new capacity will be calculated as (2*oldcapacity+2).
新容量将计算为(2 * oldcapacity + 2)。
Suppose current StringBuffer object capacity is 10 and we are allocating a new object of capacity is 26 then new space will be allocated for the newly created object.
假设当前StringBuffer对象的容量为10,并且我们正在分配容量为26的新对象,那么将为新创建的对象分配新的空间。
Syntax:
句法:
void ensureCapacity(int mincap){
}
Parameter(s):
参数:
We pass only one object in the method of the StringBuffer i.e. minimum capacity (mincap).
我们仅在StringBuffer方法中传递一个对象,即最小容量(mincap)。
Return value:
返回值:
The return type of this method is void that means this method returns nothing.
此方法的返回类型为void ,表示此方法不返回任何内容。
Java程序,演示sureCapacity()方法的示例 (Java program to demonstrate example of ensureCapacity() method)
import java.lang.StringBuffer;
public class StringBufferClass {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer(" Java supports multithreading");
System.out.println("The value of current StringBuffer object is :" + sb);
// Display the capacity of current stringbuffer object sb
System.out.println("The old capacity is :" + sb.capacity());
// Extend the capacity to the specified amount
// in the given stringbuffer object
sb.ensureCapacity(50);
// Display new extended capacity of the stringbuffer object
// Return the new capacity (2*oldcapacity+2)
System.out.println("The new capacity will be : " + sb.capacity());
}
}
Output
输出量
D:\Programs>javac StringBufferClass.java
D:\Programs>java StringBufferClass
The value of current StringBuffer object is : Java supports multithreading
The old capacity is :45
The new capacity will be : 92