java.nio.Buffer類的capacity()方法用於返回此緩衝區的容量。
用法:
public final int capacity()
返回值:該緩衝區的容量
下麵是說明capacity()方法的示例:
範例1:
// Java program to demonstrate
// capacity() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(7);
// putting the int to byte typecast
// value in ByteBuffer
bb.put((byte)20);
bb.put((byte)30);
bb.put((byte)40);
bb.put((byte)50);
// Typecasting ByteBuffer into Buffer
Buffer bb1 = (Buffer)bb;
// getting capacity of Buffer
// using capacity() method
int cap = bb1.capacity();
// display the result
System.out.println("capacity is: "
+ cap);
}
}
輸出:
capacity is: 7
範例2:
// Java program to demonstrate
// capacity() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring and initializing byte array
byte[] byt = { (byte)20, (byte)30,
(byte)40, (byte)50,
(byte)60 };
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.wrap(byt);
// Typecasting ByteBuffer into Buffer
Buffer bb1 = (Buffer)bb;
// getting capacity of Buffer
// using capacity() method
int cap = bb1.capacity();
// display the result
System.out.println("capacity is: "
+ cap);
}
}
輸出:
capacity is: 5
參考: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#capacity–
相關用法
- Java Buffer arrayOffset()用法及代碼示例
- Java Buffer duplicate()用法及代碼示例
- Java StringBuilder capacity()用法及代碼示例
- Java Stack capacity()用法及代碼示例
- Java Vector capacity()用法及代碼示例
- Java Buffer hasRemaining()用法及代碼示例
- Java Buffer isDirect()用法及代碼示例
- Java Buffer mark()用法及代碼示例
- Java Buffer position()用法及代碼示例
- Java Buffer clear()用法及代碼示例
- Java Buffer array()用法及代碼示例
- Java Buffer limit()用法及代碼示例
- Java Buffer remaining()用法及代碼示例
- Java Buffer isReadOnly()用法及代碼示例
- Java Buffer rewind()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Buffer capacity() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。