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