wrap(float[] array)
The wrap() method of java.nio.FloatBuffer Class is used to wraps a float array into a buffer. The new buffer will be backed by the given float array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer’s capacity and limit will be array.length, its position will be zero, and its mark will be undefined. It’s backing array will be the given array, and its array offset will be zero.
Syntax :
public static FloatBuffer wrap(float[] array)
Parameters: This method takes array(The array that will back this buffer) as a parameter.
Return Value: This method returns the new float buffer.
Below are the examples to illustrate the wrap() method:
Examples 1:
// Java program to demonstrate // wrap() 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[] array, int offset, int length)
The new buffer will be backed by the given float array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer’s capacity will be array.length, its position will be offset, its limit will be offset + length, and its mark will be undefined. Its backing array will be the given array, and its array offset will be zero.
Syntax :
public static FloatBuffer wrap (float[] array, int offset, int length)
Parameters: This method takes following parameters:
- array: The array that will back the new buffer.
- offset: The offset of the subarray to be used; must be non-negative and no larger than array.length. The new buffer’s position will be set to this value.
- length: The length of the subarray to be used; must be non-negative and no larger than array.length – offset. The new buffer’s limit will be set to offset + length.
Return Value: This method returns the new float buffer.
Throws: This method throws the IndexOutOfBoundsException(If the preconditions on the offset and length parameters do not hold).
Below are the examples to illustrate the wrap() method:
Examples 1:
// Java program to demonstrate // wrap() 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
Examples 2: To demonstrate NullPointerException
// Java program to demonstrate // wrap() 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