Java ByteBuffer getDouble()方法的例子
getDouble()
java.nio.ByteBuffer 类的 getDouble() 方法用于读取该缓冲区当前位置的下8个字节,根据当前的字节顺序将其合成为一个双倍值,然后将位置递增8。
语法
public abstract double getDouble()
返回值: 该方法返回缓冲区当前位置的双倍值。
抛出: 该方法抛出 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 index)
ByteBuffer 的 getDouble(int index) 方法用于在给定的索引处读取8个字节,根据当前的字节顺序将它们合成为一个双倍值。
语法:
public abstract double getDouble(int index)
参数: 该方法以index为参数,该参数是将被读取的字节的索引。
返回值: 该方法返回给定索引处的双倍值。
异常: 该方法抛出 IndexOutOfBoundsException。 如果索引为负数或不小于缓冲区的限制,就会抛出这个异常。
下面的例子说明了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-
极客教程