IntBuffer.wrap(int[], int, int): This method is available in java.nio.IntBuffer class of Java.
Syntax:
IntBuffer java.nio.IntBuffer.wrap(int[] array, int offset, int length)
This method takes three arguments one of type int array and the other two are of type int as its parameters. This method wraps an int array into a buffer.
Parameters: Three parameters are required for this method.
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 the 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 an array.length - offset. The new buffer's limit will be set to offset + length.
Returns: The new int buffer.
Throws:
IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold
Approach 1: When no exceptions
Java
import java.nio.IntBuffer;public class IntBufferwrap2 {public static void main(String[] args) {int array[] = { 1, 2, 3, 4 };int offset = 0;int length = 2;System.out.println(IntBuffer.wrap(array, offset, length));}}
Output:
java.nio.HeapIntBuffer[pos=0 lim=2 cap=4]
Approach 2: IndexOutOfBoundsException
Java
import java.nio.IntBuffer;public class IntBufferwrap2 {public static void main(String[] args) {int array[] = { 1, 2, 3, 4 };int offset = 0;int length = 6;System.out.println(IntBuffer.wrap(array, offset, length));}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.nio.IntBuffer.wrap(IntBuffer.java:408)
No comments:
Post a Comment