📌  相关文章
📜  Java中的 FloatBuffer put() 方法及示例

📅  最后修改于: 2022-05-13 01:54:42.470000             🧑  作者: Mango

Java中的 FloatBuffer put() 方法及示例

放(浮点数)

Java.nio.FloatBuffer类的put(float f)方法用于将给定的float写入当前位置新创建的float buffer中,然后增加位置。

句法 :

public abstract FloatBuffer put(float f)

参数:此方法将浮点值f作为参数写入浮点缓冲区。

返回值:此方法返回此缓冲区,其中插入了浮点值。

异常:此方法抛出以下异常:

  • 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 limit
Exception 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 buffer
Exception throws : java.nio.ReadOnlyBufferException

放(int 指数,浮动 f)

Java.nio.FloatBuffer 类的 put(int index, float f) 方法用于将给定的浮点数写入给定索引处的缓冲区。

句法:

public abstract FloatBuffer put(int index, float f)

参数:此方法采用以下参数作为参数:

  • index :将写入浮点数的索引
  • f : 要写入的浮点值

返回值:此方法返回此缓冲区。

异常:此方法抛出以下异常:

  • IndexOutOfBoundsException-如果 index 为负数或不小于缓冲区的限制
  • 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 index
Exception 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 buffer
Exception throws : java.nio.ReadOnlyBufferException