Java 中 ByteBuffer asIntBuffer() 方法
可以使用 asIntBuffer() 方法在類 java.nio.ByteBuffer 中建立 ByteBuffer 的 IntBuffer 檢視。此方法不需要引數,它將根據需要返回一個 int 緩衝區。此緩衝區反映了對原始緩衝區所做的更改,反之亦然。
演示此操作的程式如下所示 −
示例
import java.nio.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
int n = 50;
try {
ByteBuffer bufferB = ByteBuffer.allocate(n);
IntBuffer bufferI = bufferB.asIntBuffer();
bufferI.put(3);
bufferI.put(9);
bufferI.put(1);
bufferI.put(7);
bufferI.put(4);
bufferI.rewind();
int i;
System.out.print("The IntBuffer is: ");
while ((i = bufferI.get()) != 0) {
System.out.print(i + " ");
}
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}輸出
The IntBuffer is: 3 9 1 7 4
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP