LongBuffer get(long[]) in Java

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

Syntax:

This method takes one argument of type long array as its parameter. This method transfers longs 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 longs remaining in this buffer

Approach 1: When no exceptions.

Java

import java.nio.LongBuffer;

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

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

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

Output:

java.nio.HeapLongBuffer[pos=3 lim=4 cap=4]


Approach 2: BufferUnderflowException

Java

import java.nio.LongBuffer;

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

        long array[] = { 1, 2 };
        LongBuffer lb = LongBuffer.wrap(array);

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


Output:

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


No comments:

Post a Comment