java 中的 IntBuffer duplicate() 方法
可以使用 java.nio.IntBuffer 类中的 duplicate() 方法创建缓冲区的副本缓冲区。此副本缓冲区与原始缓冲区相同。duplicate() 方法返回创建的副本缓冲区。
演示此功能的程序如下 −
示例
import java.nio.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
int n = 5;
try {
IntBuffer buffer1 = IntBuffer.allocate(n);
buffer1.put(8);
buffer1.put(1);
buffer1.put(3);
buffer1.put(7);
buffer1.put(5);
buffer1.rewind();
System.out.println("The Original IntBuffer is: " + Arrays.toString(buffer1.array()));
IntBuffer buffer2 = buffer1.duplicate();
System.out.print("The Duplicate IntBuffer is: " + Arrays.toString(buffer2.array()));
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e){
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}上述程序的输出如下 −
输出
The Original IntBuffer is: [8, 1, 3, 7, 5] The Duplicate IntBuffer is: [8, 1, 3, 7, 5]
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP