當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。