Java 中 DoubleBuffer 的 slice() 方法


在 java.nio.DoubleBuffer 类中可以使用方法 slice() 创建一个新 DoubleBuffer,其内容是原始 DoubleBuffer 的共享子序列。如果原始缓冲是只读的,则此方法返回新 DoubleBuffer,该 DoubleBuffer 是只读的;如果原始缓冲是直接的,则返回的 DoubleBuffer 是直接的。

演示这一点的程序如下 −

示例

 实时演示

import java.nio.*;
import java.util.*;
public class Demo {
   public static void main(String[] args) {
      int n = 5;
      try {
         DoubleBuffer buffer1 = DoubleBuffer.allocate(n);
         buffer1.put(4.5D);
         buffer1.put(1.2D);
         buffer1.put(3.9D);
         System.out.println("The Original DoubleBuffer is: " + Arrays.toString(buffer1.array()));
         System.out.println("The position is: " + buffer1.position());
         System.out.println("The capacity is: " + buffer1.capacity());
         DoubleBuffer buffer2 = buffer1.slice();
         System.out.println("
The Subsequence DoubleBuffer is: " + Arrays.toString(buffer2.array()));          System.out.println("The position is: " + buffer2.position());          System.out.println("The capacity is: " + buffer2.capacity());       } catch (IllegalArgumentException e) {          System.out.println("Error!!! IllegalArgumentException");       } catch (ReadOnlyBufferException e) {          System.out.println("Error!!! ReadOnlyBufferException");       }    } }

上述程序的输出如下 −

输出

The Original DoubleBuffer is: [4.5, 1.2, 3.9, 0.0, 0.0]
The position is: 3
The capacity is: 5

The Subsequence DoubleBuffer is: [4.5, 1.2, 3.9, 0.0, 0.0]
The position is: 0
The capacity is: 2

更新于: 2019 年 7 月 30 日

92 次浏览

启动您的 职业生涯

完成课程获取认证

开始
广告
© . All rights reserved.