put(int, long): This method is available in java.nio.LongBuffer class of Java.
Syntax:
LongBuffer java.nio.LongBuffer.put(int index, long l)
This method takes two arguments one of type int and another of type long as its parameters. This method writes the given long into this buffer at the given index.
Parameters: Two parameters are required for this method.
index: The index at which the long will be written.
l: The long value to be written.
Returns: This buffer.
Throws:
1. IndexOutOfBoundsException - If the index is negative or not smaller than the buffer's limit.
2. ReadOnlyBufferException - If this buffer is read-only.
Approach 1: When no exceptions
Java
import java.nio.LongBuffer;public class LongBufferput4 {public static void main(String[] args) {long array[] = { 1, 2, 3, 4 };LongBuffer lb = LongBuffer.wrap(array);int index = 0;long l = 100;System.out.println(lb.put(index, l));}}
Output:
java.nio.HeapLongBuffer[pos=0 lim=4 cap=4]
Approach 2: IndexOutOfBoundsException
Java
import java.nio.LongBuffer;public class LongBufferput4 {public static void main(String[] args) {long array[] = { 1, 2, 3, 4 };LongBuffer lb = LongBuffer.wrap(array);int index = 5;long l = 100;System.out.println(lb.put(index, l));}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.nio.Buffer.checkIndex(Buffer.java:738) at java.base/java.nio.HeapLongBuffer.put(HeapLongBuffer.java:222)
Approach 3: ReadOnlyBufferException
Java
import java.nio.LongBuffer;public class LongBufferput4 {public static void main(String[] args) {long array[] = { 1, 2, 3, 4 };LongBuffer lb = LongBuffer.wrap(array);int index = 0;long l = 100;LongBuffer readOnly = lb.asReadOnlyBuffer();System.out.println(readOnly.put(index, l));}}
Output:
Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapLongBufferR.put(HeapLongBufferR.java:225)
Some more put Methods.
No comments:
Post a Comment