Open In App

DoubleBuffer wrap() method in Java with Examples

Last Updated : 27 Nov, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

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:
Output:
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:
Output:
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
Output:
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

Next Article
Practice Tags :

Similar Reads