DoubleBuffer wrap() method in Java with Examples
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
double [] fbb = { 1 .23D, 2 .34D, 4 .56D };
System.out.println( "Array length : " + fbb.length);
System.out.println( "\nArray element : "
+ Arrays.toString(fbb));
DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb);
doubleBuffer.rewind();
System.out.println( "\ndoubleBuffer : "
+ Arrays.toString(doubleBuffer.array()));
System.out.println( "\ndoublebuffer capacity : "
+ doubleBuffer.capacity());
System.out.println( "\ndoublebuffer position: "
+ doubleBuffer.position());
}
}
|
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:
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
double [] fbb = { 1 .23D, 2 .34D, 4 .56D };
System.out.println( "Array length : " + fbb.length);
System.out.println( "\nArray element : "
+ Arrays.toString(fbb));
DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb, 0 ,
fbb.length);
doubleBuffer.rewind();
System.out.println( "\ndoubleBuffer : "
+ Arrays.toString(doubleBuffer.array()));
System.out.println( "\ndoublebuffer capacity : "
+ doubleBuffer.capacity());
System.out.println( "\ndoublebuffer position: "
+ doubleBuffer.position());
}
}
|
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
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
double [] fbb = { 1 .23D, 2 .34D, 4 .56D };
System.out.println( "Array length : " + fbb.length);
System.out.println( "\nArray element : " + Arrays.toString(fbb));
try {
System.out.println( "\nHere "
+ "offset and length does not hold"
+ " the required condition " );
DoubleBuffer doubleBuffer = DoubleBuffer.wrap(fbb,
1 ,
fbb.length);
doubleBuffer.rewind();
System.out.println( "\ndoubleBuffer : "
+ Arrays.toString(doubleBuffer.array()));
System.out.println( "\ndoublebuffer capacity : "
+ doubleBuffer.capacity());
System.out.println( "\ndoublebuffer position: "
+ doubleBuffer.position());
}
catch (IndexOutOfBoundsException e) {
System.out.println( "Exception throws: " + e);
}
}
}
|
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
Last Updated :
27 Nov, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...