put(int, int): This method is available in java.nio.IntBuffer class of Java.
Syntax:
IntBuffer java.nio.IntBuffer.put(int index, int i)
This method takes two arguments of type int as its parameter. This method writes the given int into this buffer at the given index.
Parameters: Two parameters are required for this method.
index: The index at which the int will be written.
i: The int value to be written.
Returns: This buffer.
Throws:
1. IndexOutOfBoundsException - If an 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.IntBuffer;public class IntBufferput4 {public static void main(String[] args) {int array[] = { 1, 2, 3, 4 };IntBuffer lb = IntBuffer.wrap(array);int index = 0;int i = 100;System.out.println(lb.put(index, i));}}
Output:
java.nio.HeapIntBuffer[pos=0 lim=4 cap=4]
Approach 2: IndexOutOfBoundsException
Java
import java.nio.IntBuffer;public class IntBufferput4 {public static void main(String[] args) {int array[] = { 1, 2, 3, 4 };IntBuffer lb = IntBuffer.wrap(array);int index = -1;int i = 100;System.out.println(lb.put(index, i));}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.nio.Buffer.checkIndex(Buffer.java:738) at java.base/java.nio.HeapIntBuffer.put(HeapIntBuffer.java:222)
Approach 3: ReadOnlyBufferException
Java
import java.nio.IntBuffer;public class IntBufferput4 {public static void main(String[] args) {int array[] = { 1, 2, 3, 4 };IntBuffer lb = IntBuffer.wrap(array);int index = 0;int i = 100;IntBuffer readOnly = lb.asReadOnlyBuffer();System.out.println(readOnly.put(index, i));}}
Output:
Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapIntBufferR.put(HeapIntBufferR.java:225)
No comments:
Post a Comment