📜  Java中的 CharBuffer length() 方法及示例

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

Java中的 CharBuffer length() 方法及示例

Java.nio.CharBuffer 类length()方法用于返回这个字符缓冲区的长度。当被视为一个字符序列时,字符缓冲区的长度只是位置(包括)和限制(不包括)之间的字符数;也就是等价于remaining()。

句法:

public final int length()

返回值:此方法返回此字符缓冲区的长度。

以下是说明 length() 方法的示例:

示例 1:

// Java program to demonstrate
// length() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declare and initialize the char array
        char[] cb = { 'a', 'b', 'c' };
  
        // wrap the char array into CharBuffer
        // using wrap() method
        CharBuffer charBuffer = CharBuffer.wrap(cb);
  
        // Get the length of the charBuffer
        // using length() method
        int length = charBuffer.length();
  
        // print the byte buffer
        System.out.println("CharBuffer is : "
                           + Arrays.toString(
                                 charBuffer.array())
                           + "\nLength: "
                           + length);
    }
}
输出:
CharBuffer is : [a, b, c]
Length: 3

示例 2:

// Java program to demonstrate
// length() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // defining and allocating CharBuffer
        // using allocate() method
        CharBuffer charBuffer
            = CharBuffer.allocate(4);
  
        // append char value in charBuffer
        // using append() method
        charBuffer.append('a');
        charBuffer.append('b');
  
        // print the char buffer
        System.out.println("CharBuffer  before append : "
                           + Arrays.toString(
                                 charBuffer.array())
                           + "\nLength: "
                           + charBuffer.length());
  
        // append char value in charBuffer
        // using append() method
        charBuffer.append('c');
  
        // print the char buffer
        System.out.println("\nCharBuffer after append : "
                           + Arrays.toString(
                                 charBuffer.array())
                           + "\nLength: "
                           + charBuffer.length());
    }
}
输出:
CharBuffer  before append : [a, b,,  ]
Length: 2

CharBuffer after append : [a, b, c,  ]
Length: 1

参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/CharBuffer.html#length–