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)


IntBuffer get(int[]) in Java

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

Syntax:

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

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

Parameters: One parameter is required for this method.

dst: The destination array.

Returns: This buffer.

Throws:

BufferUnderflowException - If there are fewer than length ints remaining in this buffer.

Approach 1: When no exceptions.

Java

import java.nio.IntBuffer;

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

        int array[] = { 1, 2, 78, 1991, 18 };
        IntBuffer lb = IntBuffer.wrap(array);

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

Output:

java.nio.HeapIntBuffer[pos=3 lim=5 cap=5]


Approach 2: BufferUnderflowException 

Java

import java.nio.IntBuffer;

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

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

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

Output:

Exception in thread "main" java.nio.BufferUnderflowException at java.base/java.nio.HeapIntBuffer.get(HeapIntBuffer.java:185) at java.base/java.nio.IntBuffer.get(IntBuffer.java:814)


Some more get methods.


get()


get(int)


get(int, int[])


get(int[], int, int)


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


IntBuffer get(int) in Java

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

Syntax:

int java.nio.IntBuffer.get(int index)

This method takes one argument of type int as its parameter. This method reads the int at the given index.

Parameters: One parameter is required for this method.

index: The index from which the int will be read.

Returns: The int at the given index.

Throws:

IndexOutOfBoundsException - If the index is negative or not smaller than the buffer's limit.

Approach 1: When no exceptions

Java

import java.nio.IntBuffer;

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

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

        int index = 2;
        System.out.println(lb.get(index));
    }

}

Output:

3


Approach 2: IndexOutOfBoundsException 

Java

import java.nio.IntBuffer;

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

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

        int index = 5;
        System.out.println(lb.get(index));
    }

}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.nio.Buffer.checkIndex(Buffer.java:738) at java.base/java.nio.HeapIntBuffer.get(HeapIntBuffer.java:171)


Some more get methods.


get()


get(int[])


get(int, int[])


get(int[], int, int)


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


IntBuffer get() in Java

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

Syntax:

int java.nio.IntBuffer.get()

This method reads the int at this buffer'scurrent position and then increments the position.

Parameters: NA

Returns: The int at the buffer's current position.

Throws:

BufferUnderflowException - If the buffer's current position is not smaller than its limit.

Approach 1: When no exceptions

Java

import java.nio.IntBuffer;

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

        int array[] = { 12, 34, 56, 78 };
        IntBuffer lb = IntBuffer.wrap(array);

        System.out.println(lb.get());
    }
}

Output:

12


Approach 2: BufferUnderflowException

Java

import java.nio.IntBuffer;

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

        int array[] = {};
        IntBuffer lb = IntBuffer.wrap(array);

        System.out.println(lb.get());
    }
}

Output:

Exception in thread "main" java.nio.BufferUnderflowException at java.base/java.nio.Buffer.nextGetIndex(Buffer.java:694) at java.base/java.nio.HeapIntBuffer.get(HeapIntBuffer.java:166)


Some more get methods.


get(int)


get(int[])


get(int, int[])


get(int[], int, int)


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