Java中的ShortBuffer duplicate()方法及其示例

这个 重复() 方法 JAVA尼奥。短缓冲区 类创建一个共享该缓冲区内容的新短缓冲区。

null

新缓冲区的内容将是该缓冲区的内容。对该缓冲区内容的更改将在新缓冲区中显示,反之亦然;两个缓冲器的位置、极限和标记值将是独立的。新缓冲区的容量、限制、位置和标记值将与此缓冲区的相同。当且仅当此缓冲区是直接缓冲区时,新缓冲区将是直接缓冲区;当且仅当此缓冲区是只读缓冲区时,新缓冲区将是只读缓冲区。

语法 :

public abstract 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

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享