wrap(double[] array)
The wrap() method of java.nio.DoubleBuffer Class is used to wraps a double array into a buffer. The new buffer will be backed by the given double 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. Its backing array will be the given array, and its array offset will be zero.
Syntax:
public static DoubleBuffer wrap(double[] array)
Parameters: This method takes array, the array that will back this buffer, as a parameter.
Return Value: This method returns the new double buffer.
Below are the examples to illustrate the wrap() method:
// Java program to demonstrate // wrap() method import java.nio.*; import java.util.*; public class GFG { public static void main(String[] args) { // Declaire and initialize the float array double[] fbb = { 1.23D, 2.34D, 4.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.23, 2.34, 4.56] doubleBuffer : [1.23, 2.34, 4.56] doublebuffer capacity : 3 doublebuffer position: 0
wrap(double[] array, int offset, int length)
The new buffer will be backed by the given double 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 (double[] array, int offset, int length)
Parameters: This method takes the 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 double buffer.
Exception: 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 double[] fbb = { 1.23D, 2.34D, 4.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 double array into floatBuffer // using wrap() method DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb, 0, fbb.length); // Rewind the doublebuffer doubleBuffer.rewind(); // print the float buffer System.out.println("\ndoubleBuffer : " + Arrays.toString(doubleBuffer.array())); // print the FloatBuffer capacity System.out.println("\ndoublebuffer capacity : " + doubleBuffer.capacity()); // print the FloatBuffer position System.out.println("\ndoublebuffer position: " + doubleBuffer.position()); } } |
Array length : 3 Array element : [1.23, 2.34, 4.56] doubleBuffer : [1.23, 2.34, 4.56] doublebuffer capacity : 3 doublebuffer position: 0
Examples 2: To demonstrate IndexOutOfBoundsException
// 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 Double array double[] fbb = { 1.23D, 2.34D, 4.56D }; // print the Double array length System.out.println("Array length : " + fbb.length); // print the Double array element System.out.println("\nArray element : " + Arrays.toString(fbb)); try { // wrap the Double array into floatBuffer // using wrap() method System.out.println("\nHere " + "offset and length does not hold" + " the required condition "); DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb, 1, fbb.length); // Rewind the Doublebuffer doubleBuffer.rewind(); // print the Double 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()); } 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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- DoubleBuffer arrayOffset() method in Java With Examples
- DoubleBuffer compareTo() method in Java With Examples
- DoubleBuffer slice() method in Java with Examples
- DoubleBuffer asReadOnlyBuffer() method in Java with Examples
- DoubleBuffer equals() method in Java with Examples
- DoubleBuffer array() method in Java With Examples
- DoubleBuffer duplicate() method in Java with Examples
- DoubleBuffer allocate() method in Java With Examples
- DoubleBuffer compact() method in Java With Examples
- DoubleBuffer hasArray() method in Java with Examples
- DoubleBuffer rewind() Method in Java with Examples
- DoubleBuffer put() methods in Java with Examples | Set 1
- DoubleBuffer get() methods in Java with Examples
- DoubleBuffer flip() methods in Java with Examples
- DoubleBuffer clear() methods in Java with Examples
- DoubleBuffer mark() methods in Java with Examples
- DoubleBuffer limit() methods in Java with Examples
- DoubleBuffer order() methods in Java with Examples
- DoubleBuffer reset() methods in Java with Examples
- FloatBuffer wrap() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

