Java 中 FloatBuffer 的 get() 方法
使用类 java.nio.FloatBuffer 中的方法 get(),读取缓冲区当前位置的值,然后增加该值。此方法返回的是当前缓冲区位置处的值。此外,如果出现下溢情况,则会引发 BufferUnderflowException。
演示此方法的程序如下所示 -
示例
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { FloatBuffer buffer = FloatBuffer.allocate(n); buffer.put(4.5F); buffer.put(1.2F); buffer.put(3.9F); buffer.put(7.5F); buffer.put(5.8F); buffer.rewind(); System.out.println("The FloatBuffer is: " + Arrays.toString(buffer.array())); float val1 = buffer.get(); System.out.println("
The value at current position of FloatBuffer is: " + val1); float val2 = buffer.get(); System.out.println("The value at next position of FloatBuffer is: " + val2); } catch (IllegalArgumentException e) { System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } catch (BufferUnderflowException e) { System.out.println("Error!!! BufferUnderflowException"); } } }
以上程序的输出如下 -
输出
The FloatBuffer is: [4.5, 1.2, 3.9, 7.5, 5.8] The value at current position of FloatBuffer is: 4.5 The value at next position of FloatBuffer is: 1.2
广告