CharBuffer 的 get() 方法在 Java 中
在 java.nio.CharBuffer 类中,使用 get() 方法会先读取缓冲区的当前位置的值,然后再将其增加。此方法返回当前缓冲区位置的值。此外,如果发生容量不足情况,将会抛出 BufferUnderflowException。
展示这一点的程序如下 -
示例
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { CharBuffer buffer = CharBuffer.allocate(n); 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())); char val1 = buffer.get(); System.out.println("
The value at current position of CharBuffer is: " + val1); char val2 = buffer.get(); System.out.println("The value at next position of CharBuffer is: " + val2); } catch (IllegalArgumentException e) { System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } catch (BufferUnderflowException e) { System.out.println("Error!!! BufferUnderflowException"); } } }
输出
The CharBuffer is: [A, P, P, L, E] The value at current position of CharBuffer is: A The value at next position of CharBuffer is: P
广告