Java StringBuffer void sureCapacity(int mincap)方法与示例

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


翻译自: https://www.includehelp.com/java/stringbuffer-void-ensurecapacity-int-mincap-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值