put(int[], int, int): This method is available in java.nio.IntBuffer class of Java.
Syntax:
IntBuffer java.nio.IntBuffer.put(int[] src, 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 transfers ints into this buffer from the given source array.
Parameters: Three parameters are required for this method.
src: The array from which ints are to be read.
offset: The offset within the array of the first int to be read; must be non-negative and no larger than src.length.
length: The number of ints to be read from the given array; must be non-negative and no larger than src.length - offset.
Returns: This buffer.
Throws:
1. BufferOverflowException - If there is insufficient space in this buffer.
2. IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold.
3. ReadOnlyBufferException - If this buffer is read-only.
Approach 1: When no exceptions.
Java
import java.nio.IntBuffer;public class IntBufferput6 {public static void main(String[] args) {int array[] = { 1, 2, 3, 4 };IntBuffer lb = IntBuffer.wrap(array);int offset = 0;int length = 2;int src[] = { 2, 3, 4, 5, 6 };System.out.println(lb.put(src, offset, length));}}
Output:
java.nio.HeapIntBuffer[pos=2 lim=4 cap=4]
Approach 2: IndexOutOfBoundsException
Java
import java.nio.IntBuffer;public class IntBufferput6 {public static void main(String[] args) {int array[] = { 1, 2, 3, 4 };IntBuffer lb = IntBuffer.wrap(array);int offset = 2;int length = 2;int src[] = { 2, 3, 4 };System.out.println(lb.put(src, offset, length));}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [2, 2 + 2) out of bounds for length 3 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:82) at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:343) at java.base/java.util.Objects.checkFromIndexSize(Objects.java:411) at java.base/java.nio.HeapIntBuffer.put(HeapIntBuffer.java:232)
Approach 3: ReadOnlyBufferException
Java
import java.nio.IntBuffer;public class IntBufferput6 {public static void main(String[] args) {int array[] = { 1, 2, 3, 4 };IntBuffer lb = IntBuffer.wrap(array);int offset = 0;int length = 2;int src[] = { 2, 3, 4, 5, 6 };IntBuffer readOnly = lb.asReadOnlyBuffer();System.out.println(readOnly.put(src, offset, length));}}
Output:
Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapIntBufferR.put(HeapIntBufferR.java:240)
No comments:
Post a Comment