📜  Java中的Buffer capacity()方法及示例

📅  最后修改于: 2022-05-13 01:54:26.503000             🧑  作者: Mango

Java中的Buffer capacity()方法及示例

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–