IntBuffer get(int, int[]) in Java

get(int, int[]): This method is available in java.nio.IntBuffer class of Java.

Syntax:

IntBuffer java.nio.IntBuffer.get(int index, int[] dst)

This method takes two arguments one of type int and another of type int array as its parameter. This method transfers ints from this buffer into the given destination array.

Note: The position of this buffer is unchanged.

Parameters: Two parameters are required for this method.

index: The index in this buffer from which the first int will be read; must be non-negative and less than the limit().

dst: The destination array.

Returns: This buffer.

Throws:

IndexOutOfBoundsException - If an index is negative, not smaller than limit(),or limit() - index < dst.length

Approach 1: When no exceptions

Java

import java.nio.IntBuffer;

public class IntBufferget4 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        int dst[] = { 1, 2, 3 };
        int index = 0;
        System.out.println(lb.get(index, dst));
    }
}

Output:

java.nio.HeapIntBuffer[pos=0 lim=4 cap=4]


Approach 2: IndexOutOfBoundsException

Java

import java.nio.IntBuffer;

public class IntBufferget4 {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };
        IntBuffer lb = IntBuffer.wrap(array);

        int dst[] = { 1, 2, 3 };
        int index = -1;
        System.out.println(lb.get(index, dst));
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [-1, -1 + 3) out of bounds for length 4 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.get(HeapIntBuffer.java:193) at java.base/java.nio.IntBuffer.get(IntBuffer.java:898)


Some more get methods.


get()


get(int)


get(int[])


get(int[], int, int)


get(int, int[], int, int)


No comments:

Post a Comment