Java 中的 IntBuffer wrap() 方法
可以使用 java.nio.IntBuffer 类中的 wrap() 方法将 int 数组封装到缓冲区中。此方法需要一个参数,即要封装到缓冲区的数组,它会返回创建的新缓冲区。如果修改了返回的缓冲区,则数组的内容也会以类似的方式修改,反之亦然。
以下提供了一个演示此操作的程序 −
示例
import java.nio.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
try {
int[] arr = { 8, 1, 3, 7, 5 };
System.out.println("The array length is: " + arr.length);
System.out.println("Array elements are: " + Arrays.toString(arr));
IntBuffer buffer = IntBuffer.wrap(arr);
buffer.rewind();
System.out.println("
The IntBuffer is: " + Arrays.toString(buffer.array()));
System.out.println("The position is: " + buffer.position());
System.out.println("The capacity is: " + buffer.capacity());
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}上述程序的输出如下 −
输出
The array length is: 5 Array elements are: [8, 1, 3, 7, 5] The IntBuffer is: [8, 1, 3, 7, 5] The position is: 0 The capacity is: 5
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP