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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。