CharBuffer hasArray() 方法在 Java 中
使用 java.nio.CharBuffer 类中的 hasArray() 方法可以检验缓冲区是否具有可访问 char 数组的支持。如果缓冲区具有可访问 int 数组的支持,该方法返回 true,否则返回 false。
演示这一功能的程序如下 −
示例
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { CharBuffer buffer = CharBuffer.allocate(5); buffer.put('A'); buffer.put('P'); buffer.put('P'); buffer.put('L'); buffer.put('E'); buffer.rewind(); System.out.println("The CharBuffer is: " + Arrays.toString(buffer.array())); boolean flag = buffer.hasArray(); if (flag) System.out.println("The CharBuffer is backed by an array"); else System.out.println("The CharBuffer is not backed by any array"); } catch (IllegalArgumentException e) { System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } } }
输出
The CharBuffer is: [A, P, P, L, E] The CharBuffer is backed by an array
广告