当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java FloatBuffer put()用法及代码示例


put(float f)

java.nio.FloatBuffer类的put(float f)方法用于将给定的float写入当前位置的新创建的float缓冲区中,然后递增该位置。

用法:

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

put(int index, float f)

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


用法:

public abstract FloatBuffer put(int index, float f)

参数:此方法将以下参数作为参数:

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

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

异常:此方法引发以下异常:

  • 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 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


相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 FloatBuffer put() methods in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。