📌  相关文章
📜  Java中的 FloatBuffer wrap() 方法及示例

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

Java中的 FloatBuffer wrap() 方法及示例

换行(浮动 [] 数组)

Java.nio.FloatBuffer类的wrap()方法用于将浮点数组包装到缓冲区中。新缓冲区将由给定的浮点数组支持;也就是说,对缓冲区的修改将导致数组被修改,反之亦然。新缓冲区的容量和限制将为 array.length,其位置为零,其标记未定义。它的后备数组将是给定的数组,并且它的数组偏移量将为零。

句法 :

public static FloatBuffer wrap(float[] array)

参数:此方法将数组(将支持此缓冲区的数组)作为参数。

返回值:此方法返回新的浮点缓冲区。

下面是说明wrap()方法的示例:

示例 1:

// Java program to demonstrate
// asReadOnlyBuffer() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declare and initialize the float array
        float[] fbb = { 1.23F, 2.34F, 4.56F };
  
        // 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
        FloatBuffer floatBuffer = FloatBuffer.wrap(fbb);
  
        // Rewind the floatbuffer
        floatBuffer.rewind();
  
        // print the float buffer
        System.out.println("\nfloatBuffer : "
                           + Arrays.toString(floatBuffer.array()));
  
        // print the FloatBuffer capacity
        System.out.println("\nfloatbuffer capacity : "
                           + floatBuffer.capacity());
  
        // print the FloatBuffer position
        System.out.println("\nfloatbuffer position:  "
                           + floatBuffer.position());
    }
}
输出:
Array length : 3

Array element : [1.23, 2.34, 4.56]

floatBuffer : [1.23, 2.34, 4.56]

floatbuffer capacity : 3

floatbuffer position:  0

wrap(float[] 数组,int 偏移量,int 长度)

新缓冲区将由给定的浮点数组支持;也就是说,对缓冲区的修改将导致数组被修改,反之亦然。新缓冲区的容量为array.length,其位置为偏移量,其限制为偏移量+长度,其标记为未定义。它的后备数组将是给定的数组,并且它的数组偏移量将为零。

句法 :

public static FloatBuffer wrap (float[] array, int offset, int length)

参数:此方法采用以下参数:

  • 数组:将支持新缓冲区的数组。
  • offset:要使用的子数组的偏移量;必须为非负且不大于 array.length。新缓冲区的位置将设置为此值。
  • length:要使用的子数组的长度;必须为非负数且不大于 array.length – 偏移量。新缓冲区的限制将设置为偏移量 + 长度。

返回值:此方法返回新的浮点缓冲区。

抛出:此方法抛出IndexOutOfBoundsException (如果偏移量和长度参数的前提条件不成立)。

下面是说明 wrap() 方法的示例:

示例 1:

// Java program to demonstrate
// asReadOnlyBuffer() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declare and initialize the float array
        float[] fbb = { 1.23F, 2.34F, 4.56F };
  
        // 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
        FloatBuffer floatBuffer = FloatBuffer.wrap(fbb, 0,
                                                   fbb.length);
  
        // Rewind the floatbuffer
        floatBuffer.rewind();
  
        // print the float buffer
        System.out.println("\nfloatBuffer : "
                           + Arrays.toString(floatBuffer.array()));
  
        // print the FloatBuffer capacity
        System.out.println("\nfloatbuffer capacity : "
                           + floatBuffer.capacity());
  
        // print the FloatBuffer position
        System.out.println("\nfloatbuffer position:  "
                           + floatBuffer.position());
    }
}
输出:
Array length : 3

Array element : [1.23, 2.34, 4.56]

floatBuffer : [1.23, 2.34, 4.56]

floatbuffer capacity : 3

floatbuffer position:  0

示例 2:演示 NullPointerException

// Java program to demonstrate
// asReadOnlyBuffer() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Declare and initialize the float array
        float[] fbb = { 1.23F, 2.34F, 4.56F };
  
        // 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));
  
        try {
            // wrap the float array into floatBuffer
            // using wrap() method
            System.out.println("\nHere "
                               + "offset and length does not hold"
                               + " the required condition ");
            FloatBuffer floatBuffer = FloatBuffer.wrap(fbb,
                                                       1,
                                                       fbb.length);
  
            // Rewind the floatbuffer
            floatBuffer.rewind();
  
            // print the float buffer
            System.out.println("\nfloatBuffer : "
                               + Arrays.toString(floatBuffer.array()));
  
            // print the FloatBuffer capacity
            System.out.println("\nfloatbuffer capacity : "
                               + floatBuffer.capacity());
  
            // print the FloatBuffer position
            System.out.println("\nfloatbuffer position:  "
                               + floatBuffer.position());
        }
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception throws:  " + e);
        }
    }
}
输出:
Array length : 3

Array element : [1.23, 2.34, 4.56]

Here offset and length does not hold the required condition 
Exception throws:  java.lang.IndexOutOfBoundsException