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


Java ByteBuffer getDouble()用法及代碼示例


getDouble()

java.nio.ByteBuffer類的getDouble()方法用於讀取此緩衝區當前位置的下八個字節,根據當前字節順序將它們組成一個雙精度值,然後將該位置加8。

用法:

public abstract double getDouble()

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


引發:如果緩衝區的當前位置不小於其限製,則此方法引發BufferUnderflowException,然後引發此異常。

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

範例1:

// Java program to demonstrate 
// getDouble() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 16; 
  
        // Creating the ByteBuffer 
        try { 
  
            // creating object of ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
            // putting the double value in the bytebuffer 
            bb.asDoubleBuffer() 
                .put(1234.3456) 
                .put(2884.4444); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // print the ByteBuffer 
            System.out.println("Original ByteBuffer: "); 
            for (int i = 1; i <= capacity / 8; i++) 
                System.out.print(bb.getDouble() + " "); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Reads the double at this buffer's current position 
            // using getDouble() method 
            double value = bb.getDouble(); 
  
            // print the char value 
            System.out.println("\n\nByte Value: " + value); 
  
            // Reads the  char at this buffer's next position 
            // using getDouble() method 
            double value1 = bb.getDouble(); 
  
            // print the char value 
            System.out.print("\nNext Byte Value: " + value1); 
        } 
  
        catch (BufferUnderflowException e) { 
  
            System.out.println("\nException Thrown : " + e); 
        } 
    } 
}
輸出:
Original ByteBuffer: 
1234.3456 2884.4444 

Byte Value: 1234.3456

Next Byte Value: 2884.4444

範例2:

// Java program to demonstrate 
// getDouble() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 16; 
  
        // Creating the ByteBuffer 
        try { 
  
            // creating object of ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
            // putting the double value in the bytebuffer 
            bb.asDoubleBuffer() 
                .put(1234.3456) 
                .put(2884.4444); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // print the ByteBuffer 
            System.out.println("Original ByteBuffer: "); 
            for (int i = 1; i <= capacity / 8; i++) 
                System.out.print(bb.getDouble() + " "); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Reads the double at this buffer's current position 
            // using getDouble() method 
            double value = bb.getDouble(); 
  
            // print the char value 
            System.out.println("\n\nByte Value: " + value); 
  
            // Reads the  char at this buffer's next position 
            // using getDouble() method 
            double value1 = bb.getDouble(); 
  
            // print the char value 
            System.out.println("\nNext Byte Value: " + value1); 
  
            // Reads the  char at this buffer's next position 
            // using getDouble() method 
            double value2 = bb.getDouble(); 
        } 
  
        catch (BufferUnderflowException e) { 
            System.out.println("\nthere are fewer than "
                               + "eight bytes remaining in"
                               + " this buffer"); 
            System.out.println("Exception Thrown : " + e); 
        } 
    } 
}
輸出:
Original ByteBuffer: 
1234.3456 2884.4444 

Byte Value: 1234.3456

Next Byte Value: 2884.4444

there are fewer than eight bytes remaining in this buffer
Exception Thrown : java.nio.BufferUnderflowException

參考: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getDouble–

getDouble(int索引)

ByteBuffer的getDouble(int index)方法用於讀取給定索引處的八個字節,並根據當前字節順序將它們組成一個double值。

用法:

public abstract double getDouble(int index)

參數:此方法將index作為參數,即將從中讀取Byte的索引。


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

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

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

範例1:

// Java program to demonstrate 
// getDouble() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 16; 
  
        // Creating the ByteBuffer 
        try { 
  
            // creating object of ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
            // putting the double value in the bytebuffer 
            bb.asDoubleBuffer() 
                .put(1234.3456) 
                .put(2884.4444); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Declaring the variable 
            double c; 
  
            // print the ByteBuffer 
            System.out.println("Original ByteBuffer: "); 
            for (int i = 1; i <= capacity / 8; i++) 
                System.out.print(bb.getDouble() + " "); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Reads the double at this buffer's current position 
            // using getDouble() method 
            double value = bb.getDouble(0); 
  
            // print the char value 
            System.out.println("\n\nByte Value: " + value); 
  
            // Reads the  char at this buffer's next position 
            // using getDouble() method 
            double value1 = bb.getDouble(8); 
  
            // print the char value 
            System.out.print("\nNext Byte Value: " + value1); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
  
            System.out.println("\nindex is negative or "
                               + "smaller than the buffer's "
                               + "limit, minus seven"); 
            System.out.println("Exception Thrown : " + e); 
        } 
    } 
}
輸出:
Original ByteBuffer: 
1234.3456 2884.4444 

Byte Value: 1234.3456

Next Byte Value: 2884.4444

範例2:

// Java program to demonstrate 
// getDouble() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the ByteBuffer 
        int capacity = 16; 
  
        // Creating the ByteBuffer 
        try { 
  
            // creating object of ByteBuffer 
            // and allocating size capacity 
            ByteBuffer bb = ByteBuffer.allocate(capacity); 
  
            // putting the double value in the bytebuffer 
            bb.asDoubleBuffer() 
                .put(1234.3456) 
                .put(2884.4444); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Declaring the variable 
            double c; 
  
            // print the ByteBuffer 
            System.out.println("Original ByteBuffer: "); 
            for (int i = 1; i <= capacity / 8; i++) 
                System.out.print(bb.getDouble() + " "); 
  
            // rewind the Bytebuffer 
            bb.rewind(); 
  
            // Reads the double at this buffer's current position 
            // using getDouble() method 
            double value = bb.getDouble(0); 
  
            // print the char value 
            System.out.println("\n\nByte Value: " + value); 
  
            // Reads the  char at this buffer's next position 
            // using getDouble() method 
            double value1 = bb.getDouble(9); 
  
            // print the char value 
            System.out.print("\nNext Byte Value: " + value1); 
        } 
  
        catch (IndexOutOfBoundsException e) { 
  
            System.out.println("\nindex is negative or"
                               + " smaller than the buffer's"
                               + " limit, minus seven"); 
            System.out.println("Exception Thrown : " + e); 
        } 
    } 
}
輸出:
Original ByteBuffer: 
1234.3456 2884.4444 

Byte Value: 1234.3456

index is negative or smaller than the buffer's limit, minus seven
Exception Thrown : java.lang.IndexOutOfBoundsException

參考: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#getDouble-int-



相關用法


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