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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP