java的duplicate用法_Java IntBuffer duplicate()用法及代码示例

java.nio.IntBuffer类的duplicate()方法用于创建一个新的IntBuffer,它在所有方面都相同地共享给定缓冲区的内容。

用法:

public abstract IntBuffer duplicate()

返回值:此方法返回新的IntBuffer,它携带以前的IntBuffer的内容

下面是说明duplicate()方法的示例:

范例1:使用直接IntBuffer

// Java program to demonstrate duplicate() method

import java.nio.*;

import java.util.*;

public class GFG {

public static void main(String[] args)

{

// Declaring the capacity of the IntBuffer

int capacity = 10;

// Creating the IntBuffer

try {

// creating object of Intbuffer

// and allocating size capacity

IntBuffer ib1 = IntBuffer.allocate(capacity);

// putting the value in Intbuffer

ib1.put(8);

ib1.put(2, 9);

ib1.rewind();

// print the Original IntBuffer

System.out.println("Original IntBuffer:  "

+ Arrays.toString(ib1.array()));

// Creating a duplicate copy of IntBuffer

// using duplicate() method

IntBuffer ib2 = ib1.duplicate();

// print the duplicate copy of IntBuffer

System.out.print("Duplicate IntBuffer: "

+ Arrays.toString(ib2.array()));

}

catch (IllegalArgumentException e) {

System.out.println("IllegalArgumentException catched");

}

catch (ReadOnlyBufferException e) {

System.out.println("ReadOnlyBufferException catched");

}

}

}

输出:

Original IntBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]

Duplicate IntBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]

范例2:使用read-onlyintbuffer

// Java program to demonstrate

// duplicate() method

// using read-onlyIntbuffer

import java.nio.*;

import java.util.*;

public class GFG {

public static void main(String[] args)

{

// Declaring the capacity of the IntBuffer

int capacity = 10;

// Creating the IntBuffer

try {

// creating object of Intbuffer

// and allocating size capacity

IntBuffer ib1 = IntBuffer.allocate(capacity);

// putting the value in Intbuffer

ib1.put(8);

ib1.put(2, 9);

ib1.rewind();

// print the Original IntBuffer

System.out.println("Original IntBuffer:  "

+ Arrays.toString(ib1.array()));

// Creating a read-only copy of IntBuffer

// using asReadOnlyBuffer() method

IntBuffer readonly = ib1.asReadOnlyBuffer();

// print the read-only copy of IntBuffer

System.out.print("read-only IntBuffer:  ");

while (Readonly.hasRemaining())

System.out.print(readonly.get() + ", ");

System.out.println("");

// Rewinding the readonly IntBuffer

readonly.rewind();

// Creating a duplicate copy of IntBuffer

// using duplicate() method

IntBuffer ib2 = readonly.duplicate();

// print the duplicate copy of IntBuffer

System.out.print("Duplicate copy of read-only IntBuffer:  ");

while (ib2.hasRemaining())

System.out.print(ib2.get() + ", ");

System.out.println("");

}

catch (IllegalArgumentException e) {

System.out.println("IllegalArgumentException catched");

}

catch (ReadOnlyBufferException e) {

System.out.println("ReadOnlyBufferException catched");

}

}

}

输出:

Original IntBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]

Read-only IntBuffer: 8, 0, 9, 0, 0, 0, 0, 0, 0, 0,

Duplicate copy of read-only IntBuffer: 8, 0, 9, 0, 0, 0, 0, 0, 0, 0,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值