Java中的IntBuffer slice()方法
可以使用slice()类java.nio.IntBuffer中的方法创建一个新的IntBuffer,其内容作为原始IntBuffer的共享子序列。如果原始缓冲区是只读的,则此方法返回只读的新IntBuffer;如果原始缓冲区是直接的,则此方法返回Direct。
演示此的程序如下所示-
示例
import java.nio.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
int n = 5;
try {
IntBuffer buffer1 = IntBuffer.allocate(n);
buffer1.put(3);
buffer1.put(7);
buffer1.put(5);
System.out.println("The Original IntBuffer is: " + Arrays.toString(buffer1.array()));
System.out.println("The position is: " + buffer1.position());
System.out.println("The limit is: " + buffer1.limit());
IntBuffer buffer2 = buffer1.slice();
System.out.println("\nThe Subsequence IntBuffer is: " + Arrays.toString(buffer2.array()));
System.out.println("The position is: " + buffer2.position());
System.out.println("The limit is: " + buffer2.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 Subsequence IntBuffer is: [3, 7, 5, 0, 0] The position is: 0 The limit is: 2
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短