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,
相關用法
- Java LongBuffer compact()用法及代碼示例
- Java LongBuffer asReadOnlyBuffer()用法及代碼示例
- Java LongBuffer equals()用法及代碼示例
- Java LongBuffer allocate()用法及代碼示例
- Java LongBuffer hasArray()用法及代碼示例
- Java LongBuffer arrayOffset()用法及代碼示例
- Java LongBuffer wrap()用法及代碼示例
- Java LongBuffer slice()用法及代碼示例
- Java LongBuffer reset()用法及代碼示例
- Java LongBuffer mark()用法及代碼示例
- Java LongBuffer clear()用法及代碼示例
- Java LongBuffer limit()用法及代碼示例
- Java LongBuffer rewind()用法及代碼示例
- Java LongBuffer flip()用法及代碼示例
- Java CharBuffer duplicate()用法及代碼示例
注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 LongBuffer duplicate() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。