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

java.nio.LongBuffer类的duplicate()方法用于创建共享给定缓冲区内容的新Long缓冲区。

用法:

public abstract LongBuffer duplicate()

返回值:此方法返回新的Long缓冲区,该缓冲区承载了先前的Long缓冲区内容

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

范例1:使用直接Longbuffer

// Java program to demonstrate

// duplicate() method

// Using direct Longbuffer

import java.nio.*;

import java.util.*;

public class GFG {

public static void main(String[] args)

{

// Declaring the capacity of the LongBuffer

int capacity = 10;

// Creating the LongBuffer

try {

// creating object of Longbuffer

// and allocating size capacity

LongBuffer ib1 = LongBuffer.allocate(capacity);

// putting the value in Longbuffer

ib1.put(8);

ib1.put(2, 9);

ib1.rewind();

// prLong the Original LongBuffer

System.out.println("Original LongBuffer: "

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

// Creating a duplicate copy of LongBuffer

// using duplicate() method

LongBuffer ib2 = ib1.duplicate();

// prLong the duplicate copy of LongBuffer

System.out.println("Duplicate LongBuffer: "

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

}

catch (IllegalArgumentException e) {

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

}

catch (ReadOnlyBufferException e) {

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

}

}

}

输出:

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

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

范例2:使用read-onlyLongbuffer

// Java program to demonstrate

// duplicate() method

// using read-onlyLongbuffer

import java.nio.*;

import java.util.*;

public class GFG {

public static void main(String[] args)

{

// Declaring the capacity of the LongBuffer

int capacity = 10;

// Creating the LongBuffer

try {

// creating object of Longbuffer

// and allocating size capacity

LongBuffer ib1 = LongBuffer.allocate(capacity);

// putting the value in Longbuffer

ib1.put(8);

ib1.put(2, 9);

ib1.rewind();

// prLong the Original LongBuffer

System.out.println("Original LongBuffer:  "

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

// Creating a read-only copy of LongBuffer

// using asReadOnlyBuffer() method

LongBuffer readonly = ib1.asReadOnlyBuffer();

// prLong the read-only copy of LongBuffer

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

while (readonly.hasRemaining())

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

System.out.println("");

// Rewinding the readonly LongBuffer

readonly.rewind();

// Creating a duplicate copy of LongBuffer

// using duplicate() method

LongBuffer ib2 = readonly.duplicate();

// prLong the duplicate copy of LongBuffer

System.out.print("duplicate copy of read-only LongBuffer:  ");

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 LongBuffer: [8, 0, 9, 0, 0, 0, 0, 0, 0, 0]

read-only LongBuffer: 8, 0, 9, 0, 0, 0, 0, 0, 0, 0,

duplicate copy of read-only LongBuffer: 8, 0, 9, 0, 0, 0, 0, 0, 0, 0,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值