📜  Java的.nio.DoubleBuffer类在Java中

📅  最后修改于: 2022-05-13 01:55:33.712000             🧑  作者: Mango

Java的.nio.DoubleBuffer类在Java中

DoubleBuffer保存要在 I/O 操作中使用的一系列双精度值。 DoubleBuffer 类对长缓冲区提供以下四类操作:

  • 读取单打的绝对和相对 get 方法。
  • 写单双打的绝对和相对放置方法。
  • 相对批量 put 和 get 方法将连续双精度序列从 int 数组或其他一些双精度缓冲区传输到此缓冲区,并从此缓冲区传输到数组。

可以通过以下方式创建短缓冲区:

  • 分配():这为缓冲区的内容分配空间。
  • wrap():将现有的长数组包装到缓冲区中。

DoubleBuffer 类的大部分方法都直接类似于 ByteBuffer 定义的方法。

类声明:

ShortBuffer 类的方法:

MethodDescription
allocate(int capacity)This method allocates a new double buffer.
array()This method returns the double array that backs this buffer. 
arrayOffset()This method returns the offset within this buffer’s backing array of the first element of the buffer.
asReadOnlyBuffer()This method creates a new, read-only short buffer that shares this buffer’s content.
compact()This method compacts this buffer.
compareTo(DoubleBuffer that)This method compares this buffer to another.
duplicate()This method creates a new double buffer that shares this buffer’s content.
equals(Object ob)This method tells whether this buffer is equal to another object.
get()This method is a relative get method and returns the double at the buffer’s current position.
get(double[] dst)This method is a relative bulk get method and returns this buffer.
get(double[] dst, int offset, int length)This method is a relative bulk get method and this buffer.
get(int index)This method is an absolute get method and this buffer.
hasArray()This method tells whether this buffer is backed by an accessible int array.
hashCode()This method returns the current hash code of this buffer.
isDirect()This method tells whether this int buffer is direct.
order()This method retrieves this buffer’s byte order.
put(double d)This method is a relative put method and returns this buffer.
put(double[] src)This method is a relative bulk put method and returns this buffer.
put(double[] src, int offset, int length)This method is a relative bulk put method and returns this buffer.
put(DoubleBuffer src)This method is a relative bulk put method and returns this buffer.
put(int index, double d)This method is an absolute put method for this buffer.
slice()This method creates a new double buffer whose content is a shared subsequence of this buffer’s content.
toString()This method returns a string summarizing the state of this buffer.
wrap(double[] array)This method wraps a double array into a buffer.
wrap(double[] array, int offset, int length)This method wraps a double array into a buffer.

以下是一些演示 DoubleBuffer 类及其方法的程序:

示例 1:

Java
// Java program to demonstrate
// DoubleBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the capacity of the DoubleBuffer
        int capacity = 5;
  
        // Creating the DoubleBuffer
  
        // creating object of Doublebuffer
        // and allocating size capacity
        DoubleBuffer db = DoubleBuffer.allocate(capacity);
  
        // putting the value in Doublebuffer
        db.put(9.26F);
        db.put(2, 5.61F);
  
        System.out.println("DoubleBuffer: "
                           + Arrays.toString(db.array()));
    }
}


Java
// Java program to demonstrate
// DoubleBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaire and initialize the float array
        double[] fbb = { 1.25D, 5.34D, 8.56D };
  
        // print the float array length
        System.out.println("Array length : " + fbb.length);
  
        // print the float array element
        System.out.println("\nArray element : "
                           + Arrays.toString(fbb));
  
        // wrap the float array into floatBuffer
        // using wrap() method
        DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb);
  
        // Rewind the floatbuffer
        doubleBuffer.rewind();
  
        // print the float buffer
        System.out.println(
            "\ndoubleBuffer : "
            + Arrays.toString(doubleBuffer.array()));
  
        // print the DoubleBuffer capacity
        System.out.println("\ndoublebuffer capacity : "
                           + doubleBuffer.capacity());
  
        // print the DoubleBuffer position
        System.out.println("\ndoublebuffer position: "
                           + doubleBuffer.position());
    }
}


输出
DoubleBuffer: [9.260000228881836, 0.0, 5.610000133514404, 0.0, 0.0]

示例 2:

Java

// Java program to demonstrate
// DoubleBuffer class
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declaire and initialize the float array
        double[] fbb = { 1.25D, 5.34D, 8.56D };
  
        // print the float array length
        System.out.println("Array length : " + fbb.length);
  
        // print the float array element
        System.out.println("\nArray element : "
                           + Arrays.toString(fbb));
  
        // wrap the float array into floatBuffer
        // using wrap() method
        DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb);
  
        // Rewind the floatbuffer
        doubleBuffer.rewind();
  
        // print the float buffer
        System.out.println(
            "\ndoubleBuffer : "
            + Arrays.toString(doubleBuffer.array()));
  
        // print the DoubleBuffer capacity
        System.out.println("\ndoublebuffer capacity : "
                           + doubleBuffer.capacity());
  
        // print the DoubleBuffer position
        System.out.println("\ndoublebuffer position: "
                           + doubleBuffer.position());
    }
}
输出
Array length : 3

Array element : [1.25, 5.34, 8.56]

doubleBuffer : [1.25, 5.34, 8.56]

doublebuffer capacity : 3

doublebuffer position: 0