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