📌  相关文章
📜  Java中的 ShortBuffer duplicate() 方法及示例

📅  最后修改于: 2022-05-13 01:55:01.750000             🧑  作者: Mango

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

Java.nio.ShortBuffer类的duplicate()方法创建了一个共享此缓冲区内容的新短缓冲区。

新缓冲区的内容就是这个缓冲区的内容。此缓冲区内容的更改将在新缓冲区中可见,反之亦然;两个缓冲区的位置、限制和标记值将是独立的。新缓冲区的容量、限制、位置和标记值将与此缓冲区的相同。当且仅当此缓冲区是直接的时,新缓冲区将是直接的,并且当且仅当此缓冲区是只读的时,它将是只读的。

语法

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

方案二

// 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