当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ShortBuffer duplicate()用法及代码示例


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


相关用法


注:本文由纯净天空筛选整理自IshwarGupta大神的英文原创作品 ShortBuffer duplicate() method in Java With Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。