Java FloatBuffer put()方法及例子
put(float f)
java.nio.FloatBuffer 类的 put(float f) 方法用于将给定的浮点数写入新创建的浮点数缓冲区的当前位置,然后递增该位置。
语法:
public abstract FloatBuffer put(float f)
参数: 该方法以Float值f作为参数,将其写入Float缓冲区。
返回值: 该方法返回这个缓冲区,在这个缓冲区中插入了浮点值。
异常: 该方法会抛出以下异常。
BufferOverflowException- 如果这个缓冲区的当前位置不小于它的极限值。ReadOnlyBufferException- 如果这个缓冲区是只读的。下面是说明put(float f)方法的例子。
例1 :
// Java program to demonstrate// put() method import java.nio.*;import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the FloatBuffer int capacity = 3; // Creating the FloatBuffer try { // creating object of floatbuffer // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity); // putting the value in floatbuffer using put() method fb.put(8.56F); fb.put(9.61F); fb.put(7.86F); fb.rewind(); // print the FloatBuffer System.out.println("Original FloatBuffer: " + Arrays.toString(fb.array())); } catch (BufferOverflowException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } }}
输出。
Original FloatBuffer: [8.56, 9.61, 7.86]
例2: 为了证明BufferOverflowException。
// Java program to demonstrate// put() method import java.nio.*;import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the FloatBuffer int capacity = 3; // Creating the FloatBuffer try { // creating object of floatbuffer // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity); // putting the value in floatbuffer using put() method fb.put(8.56F); fb.put(9.61F); fb.put(7.86F); System.out.println("Trying to put the float at the " + "position more than its limit"); fb.put(7.86F); // rewind the floatbuffer fb.rewind(); // print the FloatBuffer System.out.println("Original FloatBuffer: " + Arrays.toString(fb.array())); } catch (BufferOverflowException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } }}输出。
Trying to put the float at the position more than its limitException throws : java.nio.BufferOverflowException
实例3: 为了证明ReadOnlyBufferException。
// Java program to demonstrate// put() method import java.nio.*;import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the FloatBuffer int capacity = 3; // Creating the FloatBuffer try { // creating object of floatbuffer // and allocating size capacity using allocate() method FloatBuffer fb = FloatBuffer.allocate(capacity); // Creating a read-only copy of FloatBuffer // using asReadOnlyBuffer() method FloatBuffer fb1 = fb.asReadOnlyBuffer(); System.out.println("Trying to put the float value" + " in read only buffer"); // putting the value in readonly floatbuffer // using put() method fb1.put(8.56F); } catch (BufferOverflowException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } }}输出。
Trying to put the float value in read only bufferException throws : java.nio.ReadOnlyBufferException
put(int index, float f)
java.nio.FloatBuffer类的put(int index, float f)方法是用来将给定的浮点数写入给定索引的缓冲区中。
语法
public abstract FloatBuffer put(int index, float f)
参数: 该方法需要以下参数作为参数。
index : 将被写入浮点数的索引f : 将要写入的Float值返回值 :该方法返回这个缓冲区。
异常: 该方法会抛出以下异常。
IndexOutOfBoundsException- 如果索引是负数或者不小于缓冲区的极限值。ReadOnlyBufferException- 如果这个缓冲区是只读的。下面是一些例子来说明put(int index, float f)方法。
例1 :
// Java program to demonstrate// put() method import java.nio.*;import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the FloatBuffer int capacity = 3; // Creating the FloatBuffer try { // creating object of floatbuffer // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity); // putting the value in floatbuffer using put() at index 0 fb.put(0, 8.56F); // putting the value in floatbuffer using put() at index 2 fb.put(2, 9.61F); // putting the value in floatbuffer using put() at index 1 fb.put(1, 7.86F); // rewinding the floatbuffer fb.rewind(); // print the FloatBuffer System.out.println("Original FloatBuffer: " + Arrays.toString(fb.array())); } catch (IndexOutOfBoundsException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } }}输出。
Original FloatBuffer: [8.56, 7.86, 9.61]
例2: 演示IndexOutOfBoundsException。
// Java program to demonstrate// put() method import java.nio.*;import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the FloatBuffer int capacity = 3; // Creating the FloatBuffer try { // creating object of floatbuffer // and allocating size capacity FloatBuffer fb = FloatBuffer.allocate(capacity); // putting the value in floatbuffer // using put() at index 0 fb.put(0, 8.56F); // putting the value in floatbuffer // using put() at index 2 fb.put(2, 9.61F); // putting the value in floatbuffer // using put() at index -1 System.out.println("Trying to put the value" + " at the negative index"); fb.put(-1, 7.86F); } catch (IndexOutOfBoundsException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } }}输出。
Trying to put the value at the negative indexException throws : java.lang.IndexOutOfBoundsException
例3: 演示ReadOnlyBufferException。
// Java program to demonstrate// put() method import java.nio.*;import java.util.*; public class GFG { public static void main(String[] args) { // Declaring the capacity of the FloatBuffer int capacity = 3; // Creating the FloatBuffer try { // creating object of floatbuffer // and allocating size capacity using allocate() method FloatBuffer fb = FloatBuffer.allocate(capacity); // Creating a read-only copy of FloatBuffer // using asReadOnlyBuffer() method FloatBuffer fb1 = fb.asReadOnlyBuffer(); System.out.println("Trying to put the float value" + " in read only buffer"); // putting the value in readonly floatbuffer // using put() method fb1.put(0, 8.56F); } catch (BufferOverflowException e) { System.out.println("Exception throws : " + e); } catch (ReadOnlyBufferException e) { System.out.println("Exception throws : " + e); } }}输出。
Trying to put the float value in read only bufferException throws : java.nio.ReadOnlyBufferException
