Java 中的DoubleBuffer duplicate() 方法
可以使用类 java.nio.DoubleBuffer 中的方法 duplicate() 来创建某个缓冲区的副本缓冲区。这个副本缓冲区与原始缓冲区相同。方法 duplicate() 返回创建的副本缓冲区。
演示这一操作的程序如下所示 −
示例
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { DoubleBuffer buffer1 = DoubleBuffer.allocate(5); buffer1.put(4.5D); buffer1.put(1.2D); buffer1.put(3.9D); buffer1.put(7.5D); buffer1.put(5.8D); buffer1.rewind(); System.out.println("The Original DoubleBuffer is: " + Arrays.toString(buffer1.array())); DoubleBuffer buffer2 = buffer1.duplicate(); System.out.print("The Duplicate DoubleBuffer is: " + Arrays.toString(buffer2.array())); } catch (IllegalArgumentException e) { System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } } }
上面程序的输出如下 −
输出
The Original DoubleBuffer is: [4.5, 1.2, 3.9, 7.5, 5.8] The Duplicate DoubleBuffer is: [4.5, 1.2, 3.9, 7.5, 5.8]
广告