Java 中的 ShortBuffer duplicate() 方法
使用 java.nio.ShortBuffer 中的 duplicate() 方法可以创建缓冲区的副本缓冲区。此副本缓冲区与原始缓冲区相同。duplicate() 方法返回已创建的副本缓冲区。
演示这一点的程序如下 -
示例
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { ShortBuffer buffer1 = ShortBuffer.allocate(5); buffer1.put((short)12); buffer1.put((short)91); buffer1.put((short)25); buffer1.put((short)18); buffer1.put((short)30); buffer1.rewind(); System.out.println("The Original ShortBuffer is: " + Arrays.toString(buffer1.array())); ShortBuffer buffer2 = buffer1.duplicate(); System.out.print("The Duplicate ShortBuffer is: " + Arrays.toString(buffer2.array())); } catch (IllegalArgumentException e){ System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } } }
上述程序的输出如下 -
输出
The Original ShortBuffer is: [12, 91, 25, 18, 30] The Duplicate ShortBuffer is: [12, 91, 25, 18, 30]
广告