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)


No comments:

Post a Comment