Java中的IntBufferplicate()方法

可以使用duplicate()java.nio.IntBuffer类中的方法创建缓冲区的重复缓冲区。此重复缓冲区与原始缓冲区相同。该方法duplicate()返回创建的重复缓冲区。

演示此的程序如下所示-

示例

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(8);
         buffer1.put(1);
         buffer1.put(3);
         buffer1.put(7);
         buffer1.put(5);
         buffer1.rewind();
         System.out.println("The Original IntBuffer is: " + Arrays.toString(buffer1.array()));
         IntBuffer buffer2 = buffer1.duplicate();
         System.out.print("The Duplicate IntBuffer is: " + Arrays.toString(buffer2.array()));
      } catch (IllegalArgumentException e) {
         System.out.println("Error!!! IllegalArgumentException");
      } catch (ReadOnlyBufferException e){
         System.out.println("Error!!! ReadOnlyBufferException");
      }
   }
}

上面程序的输出如下-

输出结果

The Original IntBuffer is: [8, 1, 3, 7, 5]
The Duplicate IntBuffer is: [8, 1, 3, 7, 5]