IntBuffer compact() 方法在 Java 中
可以使用 java.nio.IntBuffer 类中的 compact() 方法压缩缓冲区。此方法不需要参数,并且它会返回具有与原始缓冲区相同内容的新压缩 IntBuffer。如果缓冲区是只读的,则会抛出 ReadOnlyBufferException。
演示这一点的程序如下 −
示例
import java.nio.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
int n = 5;
try {
IntBuffer buffer = IntBuffer.allocate(n);
buffer.put(3);
buffer.put(7);
buffer.put(5);
System.out.println("The Original IntBuffer is: " + Arrays.toString(buffer.array()));
System.out.println("The position is: " + buffer.position());
System.out.println("The limit is: " + buffer.limit());
IntBuffer bufferCompact = buffer.compact();
System.out.println("
The Compacted IntBuffer is: " + Arrays.toString(bufferCompact.array()));
System.out.println("The position is: " + bufferCompact.position());
System.out.println("The limit is: " + bufferCompact.limit());
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}以上程序的输出如下 −
输出
The Original IntBuffer is: [3, 7, 5, 0, 0] The position is: 3 The limit is: 5 The Compacted IntBuffer is: [0, 0, 5, 0, 0] The position is: 2 The limit is: 5
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP