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


Java FloatBuffer get()用法及代碼示例


get()

java.nio.FloatBuffer類的get()方法用於讀取給定緩衝區當前位置的浮點數,然後遞增該位置。

用法:

public abstract float get()

返回值:此方法返回緩衝區當前位置的浮點值。


拋出:此方法拋出BufferUnderflowException-如果緩衝區的當前位置不小於其限製,則拋出此異常。

下麵是說明get()方法的示例:

範例1:

// Java program to demonstrate 
// get() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the FloatBuffer 
        int capacity = 5; 
  
        // Creating the FloatBuffer 
        try { 
  
            // creating object of floatbuffer 
            // and allocating size capacity 
            FloatBuffer fb = FloatBuffer.allocate(capacity); 
  
            // putting the value in floatbuffer 
            fb.put(8.56F); 
            fb.put(9.61F); 
            fb.put(1.24F); 
            fb.rewind(); 
  
            // print the FloatBuffer 
            System.out.println("Original FloatBuffer:  "
                               + Arrays.toString(fb.array())); 
  
            // Reads the float at this buffer's current position 
            // using get() method 
            float value = fb.get(); 
  
            // print the Float value 
            System.out.println("\nFloat Value:" + value); 
  
            // Reads the  float at this buffer's next position 
            // using get() method 
            float value1 = fb.get(); 
  
            // print the Float value 
            System.out.print("\nNext Float Value:" + value1); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("\nIllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("\nReadOnlyBufferException catched"); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("\nException throws:" + e); 
        } 
    } 
}
輸出:
Original FloatBuffer: [8.56, 9.61, 1.24, 0.0, 0.0]

Float Value:8.56

Next Float Value:9.61

範例2:

// Java program to demonstrate 
// get() 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 
            fb.put(8.56F); 
            fb.put(9.61F); 
  
            // print the FloatBuffer 
            System.out.println("Original FloatBuffer:  "
                               + Arrays.toString(fb.array())); 
  
            // Reads the float at this buffer's current position 
            // using get() method 
            float value = fb.get(); 
  
            // print the Float value 
            System.out.println("\nFloat Value:" + value); 
  
            // Reads the  float at this buffer's next position 
            // using get() method 
            System.out.print("\nsince the buffer current position is incremented"); 
            System.out.print(" to greater than its limit "); 
  
            float value1 = fb.get(); 
  
            // print the Float value 
            System.out.print("\nNext Float Value:" + value1); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("\nIllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("\nReadOnlyBufferException catched"); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("\nException throws:" + e); 
        } 
    } 
}
輸出:
Original FloatBuffer: [8.56, 9.61, 0.0]

Float Value:0.0

since the buffer current position is incremented to greater than its limit 
Exception throws:java.nio.BufferUnderflowException

get(int index)

FloatBuffer的get(int index)方法用於讀取指定索引處的文章。

用法:

public abstract float get(int index)

參數:此方法將索引(將從其讀取浮點數的索引)作為參數。

返回值:此方法返回給定索引處的float值。


異常:此方法引發IndexOutOfBoundsException。如果index為負或不小於緩衝區的限製,則拋出此異常。

以下示例說明了get(int index)方法:

範例1:

// Java program to demonstrate 
// get(int index) 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 
            fb.put(8.56F); 
            fb.put(9.61F); 
            fb.put(6.61F); 
  
            // print the FloatBuffer 
            System.out.println("Original FloatBuffer:  "
                               + Arrays.toString(fb.array())); 
  
            // Reads the float at the index 0 of the floatbuffer 
            // using get() method 
            float value0 = fb.get(0); 
  
            // print the Float value 
            System.out.println("\nFloat Value at index 0:" + value0); 
  
            // Reads the float at the index 1 of the floatbuffer 
            // using get() method 
            float value1 = fb.get(1); 
  
            // print the Float value 
            System.out.println("\nFloat Value at index 1:" + value1); 
  
            // Reads the float at the index 2 of the floatbuffer 
            // using get() method 
            float value2 = fb.get(2); 
  
            // print the Float value 
            System.out.println("\nFloat Value at index 2:" + value2); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("\nIllegalArgumentException catched"); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
  
            System.out.println("\nReadOnlyBufferException catched"); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("\nException throws:" + e); 
        } 
    } 
}
輸出:
Original FloatBuffer: [8.56, 9.61, 6.61]

Float Value at index 0:8.56

Float Value at index 1:9.61

Float Value at index 2:6.61

範例2:

// Java program to demonstrate 
// get() 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 
            fb.put(8.56F); 
            fb.put(9.61F); 
            fb.put(6.61F); 
  
            // print the FloatBuffer 
            System.out.println("Original FloatBuffer:  "
                               + Arrays.toString(fb.array())); 
  
            // Reads the float at the index 0 of the floatbuffer 
            // using get() method 
            float value0 = fb.get(0); 
  
            // print the Float value 
            System.out.println("\nFloat Value at index 0:" + value0); 
  
            // Reads the float at the index 1 of the floatbuffer 
            // using get() method 
            float value1 = fb.get(1); 
  
            // print the Float value 
            System.out.println("\nFloat Value at index 1:" + value1); 
  
            // Reads the float at the index 2 of the floatbuffer 
            // using get() method 
            System.out.println("\nTrying to get the float"
                               + " of index greater than its limit "); 
            float value2 = fb.get(4); 
  
            // print the Float value 
            System.out.println("\nFloat Value at index 2:" + value2); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
  
            System.out.println("Exception thrown:" + e); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Original FloatBuffer: [8.56, 9.61, 6.61]

Float Value at index 0:8.56

Float Value at index 1:9.61

Trying to get the float of index greater than its limit 
Exception thrown:java.lang.IndexOutOfBoundsException


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 FloatBuffer get() methods in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。