獲取 Java 中 StringBuffer 物件的容量
capacity() 方法是 StringBuffer 類的其中一個方法。它會返回為新插入字元提供儲存容量的數量,儲存分配完成後會發生此過程。
宣告 - java.lang.StringBuffer.capacity() 方法的宣告如下:−
public int capacity()
我們來看一個示例程式,瞭解如何獲取 StringBuffer 物件的容量。
示例
import java.lang.*;
public class Example {
public static void main(String[] args) {
StringBuffer b = new StringBuffer(“Hello”);
// returns the current capacity of the String buffer which is 16 + 5 = 21
System.out.println("Capacity for the StringBuffer Object = " + b.capacity());
}
}輸出
Capacity for the StringBuffer Object = 21
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP