在 Java 中为 StringBuffer 对象设置容量值
capacity() 方法返回当前容量。它是 StringBuffer 的一部分。它是为最近插入的字符存在的存储量,此后会进行分配。
声明 - capacity() 方法在 java.lang.StringBuffer 类中声明,如下所示:-
public int capacity()
StringBuffer 对象的容量可以通过在实例化 StringBuffer 类时插入要设置的值来设置。
我们来看一个示例程序,展示如何设置容量值。
示例
import java.lang.*;
public class Example {
public static void main(String[] args) {
StringBuffer b = new StringBuffer(100);
System.out.println("Capacity for the StringBuffer Object = " + b.capacity());
b = new StringBuffer(" ");
// returns the current capacity of the String buffer which is 16 + 1
System.out.println("Capacity for the StringBuffer Object = " + b.capacity());
}
}输出
Capacity for the StringBuffer Object = 100 Capacity for the StringBuffer Object = 17
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP