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


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


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

用法:

public abstract double get()

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


異常:如果緩衝區的當前位置不小於其限製,則此方法將引發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 
            DoubleBuffer fb 
                = DoubleBuffer.allocate(capacity); 
  
            // putting the value in Doublebuffer 
            fb.put(8.56D); 
            fb.put(9.61D); 
            fb.put(1.24D); 
            fb.rewind(); 
  
            // print the DoubleBuffer 
            System.out.println("Original DoubleBuffer:  "
                               + Arrays.toString(fb.array())); 
  
            // Reads the double at this buffer's current position 
            // using get() method 
            double value = fb.get(); 
  
            // print the double value 
            System.out.println("\nDouble Value:"
                               + value); 
  
            // Reads the  double at this buffer's next position 
            // using get() method 
            double value1 = fb.get(); 
  
            // print the double value 
            System.out.print("\nNext double 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 DoubleBuffer: [8.56, 9.61, 1.24, 0.0, 0.0]

Double Value:8.56

Next double 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 DoubleBuffer 
        int capacity = 3; 
  
        // Creating the DoubleBuffer 
        try { 
  
            // creating object of Doublebuffer 
            // and allocating size capacity 
            DoubleBuffer fb = DoubleBuffer.allocate(capacity); 
  
            // putting the value in Doublebuffer 
            fb.put(8.56F); 
            fb.put(9.61F); 
  
            // print the DoubleBuffer 
            System.out.println("Original DoubleBuffer:  "
                               + Arrays.toString(fb.array())); 
  
            // Reads the Double at this buffer's current position 
            // using get() method 
            Double value = fb.get(); 
  
            // print the Double value 
            System.out.println("\nDouble Value:" + value); 
  
            // Reads the  Double 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 "); 
  
            Double value1 = fb.get(); 
  
            // print the Double value 
            System.out.print("\nNext Double 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 DoubleBuffer: [8.5600004196167, 9.609999656677246, 0.0]

Double Value:0.0

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

參考: https://docs.oracle.com/javase/9/docs/api/java/nio/DoubleBuffer.html#get-



相關用法


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