Java ShortBuffer duplicate()方法及示例

Java ShortBuffer duplicate()方法及示例

java.nio.ShortBuffer 类的 replicate() 方法创建一个新的短缓冲区,共享这个缓冲区的内容。新的缓冲区的内容将是这个缓冲区的内容。这个缓冲区的内容的变化将在新的缓冲区中可见,反之亦然;两个缓冲区的位置、极限和标记值将是独立的。新缓冲区的容量、极限、位置和标记值将与这个缓冲区的相同。当且仅当这个缓冲区是直接的,新的缓冲区将是直接的;当且仅当这个缓冲区是只读的,它将是只读的。

语法:

public abstract ShortBuffer duplicate()

返回值 :该方法返回 新的ShortBuffer

下面的程序说明了 复制() 方法。

程序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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程