Java中的DoubleBuffer slice()方法
可以使用slice()
类java.nio.DoubleBuffer中的方法创建一个新的DoubleBuffer,其内容作为原始DoubleBuffer的共享子序列。如果原始缓冲区是只读的,则此方法返回只读的新DoubleBuffer;如果原始缓冲区是直接的,则此方法返回Direct。
演示此的程序如下所示-
示例
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { DoubleBuffer buffer1 = DoubleBuffer.allocate(n); buffer1.put(4.5D); buffer1.put(1.2D); buffer1.put(3.9D); System.out.println("The Original DoubleBuffer is: " + Arrays.toString(buffer1.array())); System.out.println("The position is: " + buffer1.position()); System.out.println("The capacity is: " + buffer1.capacity()); DoubleBuffer buffer2 = buffer1.slice(); System.out.println("\nThe Subsequence DoubleBuffer is: " + Arrays.toString(buffer2.array())); System.out.println("The position is: " + buffer2.position()); System.out.println("The capacity is: " + buffer2.capacity()); } 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, 0.0, 0.0] The position is: 3 The capacity is: 5 The Subsequence DoubleBuffer is: [4.5, 1.2, 3.9, 0.0, 0.0] The position is: 0 The capacity is: 2