compact(): This method is available in java.nio.IntBuffer class of Java.
Syntax:
IntBuffer java.nio.IntBuffer.compact()
This method compacts this buffer.
Parameters: NA
Returns: This buffer.
Throws:
ReadOnlyBufferException - If this buffer is read-only.
Approach 1: When no exceptions
Java
import java.nio.IntBuffer;public class IntBuffercompact {public static void main(String[] args) {int array[] = { 1, 2, 3, 4 };IntBuffer lb = IntBuffer.wrap(array);System.out.println(lb.compact());}}
Output:
java.nio.HeapIntBuffer[pos=4 lim=4 cap=4]
Approach 2: ReadOnlyBufferException
Java
import java.nio.IntBuffer;public class IntBuffercompact {public static void main(String[] args) {int array[] = { 1, 2, 3, 4 };IntBuffer lb = IntBuffer.wrap(array);IntBuffer readOnly = lb.asReadOnlyBuffer();System.out.println(readOnly.compact());}}
Output:
Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapIntBufferR.compact(HeapIntBufferR.java:296)
No comments:
Post a Comment