java.nio.ShortBuffer類的duplicate()方法創建一個共享該緩衝區內容的新的short緩衝區。
新緩衝區的內容就是該緩衝區的內容。對該緩衝區內容的更改將在新緩衝區中可見,反之亦然;這兩個緩衝區的位置,限製和標記值將是獨立的。新緩衝區的容量,限製,位置和標記值將與此緩衝區相同。當且僅當該緩衝區是直接緩衝區時,新緩衝區才是直接緩衝區;當且僅當該緩衝區是隻讀緩衝區時,新緩衝區才是隻讀緩衝區。
用法:
public abstract ShortBuffer duplicate()
返回值:此方法返回新的ShortBuffer。
以下示例程序旨在說明duplicate()方法:
程序1:
// Java program to demonstrate
// compareTo() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the sb
int capacity1 = 10;
// Creating the ShortBuffer
// creating object of shortbuffer sb
// and allocating size capacity
ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
// putting the value in sb
sb1.put((short)100);
sb1.put((short)400);
sb1.put((short)210);
// revind the short buffer
sb1.rewind();
// print the Original ShortBuffer
System.out.println("Original ShortBuffer: "
+ Arrays.toString(sb1.array()));
// creating duplicate
ShortBuffer sb2 = sb1.duplicate();
// print the duplicate copy of ShortBuffer
System.out.println("Duplicate ShortBuffer: "
+ Arrays.toString(sb2.array()));
// checking if the duplicate is correct or not
System.out.println("are sb1 and sb2 equal: "
+ sb1.equals(sb2));
}
}
輸出:
Original ShortBuffer: [100, 400, 210, 0, 0, 0, 0, 0, 0, 0] Duplicate ShortBuffer: [100, 400, 210, 0, 0, 0, 0, 0, 0, 0] are sb1 and sb2 equal: true
程序2:
// Java program to demonstrate
// compareTo() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the sb
int capacity1 = 10;
// Creating the ShortBuffer
// creating object of shortbuffer sb
// and allocating size capacity
ShortBuffer sb1 = ShortBuffer.allocate(capacity1);
ShortBuffer sb3 = ShortBuffer.allocate(capacity1);
// putting the value in sb
sb1.put((short)100);
sb1.put((short)400);
sb1.put((short)210);
// revind the short buffer
sb1.rewind();
// print the Original ShortBuffer
System.out.println("Original ShortBuffer: "
+ Arrays.toString(sb1.array()));
// creating duplicate
ShortBuffer sb2 = sb1.duplicate();
// print the duplicate copy of ShortBuffer
System.out.println("Duplicate ShortBuffer: "
+ Arrays.toString(sb2.array()));
// checking if the duplicate is correct or not
System.out.println("are sb1 and sb2 equal: "
+ sb1.equals(sb2));
// checking if the duplicate is correct or not
System.out.println("are sb2 and sb3 equal: "
+ sb2.equals(sb3));
}
}
輸出:
Original ShortBuffer: [100, 400, 210, 0, 0, 0, 0, 0, 0, 0] Duplicate ShortBuffer: [100, 400, 210, 0, 0, 0, 0, 0, 0, 0] are sb1 and sb2 equal: true are sb2 and sb3 equal: false
相關用法
- Java ShortBuffer equals()用法及代碼示例
- Java ShortBuffer toString()用法及代碼示例
- Java ShortBuffer isDirect()用法及代碼示例
- Java ShortBuffer hashCode()用法及代碼示例
- Java ShortBuffer put(int, short)用法及代碼示例
- Java ShortBuffer allocate()用法及代碼示例
- Java ShortBuffer array()用法及代碼示例
- Java ShortBuffer limit()用法及代碼示例
- Java ShortBuffer slice()用法及代碼示例
- Java ShortBuffer reset()用法及代碼示例
- Java ShortBuffer hasArray()用法及代碼示例
- Java ShortBuffer rewind()用法及代碼示例
- Java ShortBuffer mark()用法及代碼示例
- Java ShortBuffer order()用法及代碼示例
- Java ShortBuffer arrayOffset()用法及代碼示例
注:本文由純淨天空篩選整理自IshwarGupta大神的英文原創作品 ShortBuffer duplicate() method in Java With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。