當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java DoubleBuffer duplicate()用法及代碼示例


java.nio.DoubleBuffer類的duplicate()方法用於創建共享給定緩衝區內容的新浮點緩衝區。

新緩衝區的內容就是該緩衝區的內容。對該緩衝區內容的更改將在新緩衝區中可見,反之亦然;這兩個緩衝區的位置,限製和標記值將是獨立的。

新緩衝區的容量,限製,位置和標記值將與此緩衝區相同。當且僅當該緩衝區是直接緩衝區時,新緩衝區才是直接緩衝區;當且僅當該緩衝區是隻讀緩衝區時,新緩衝區才是隻讀緩衝區。


用法:

public abstract DoubleBuffer duplicate()

返回值:此方法返回新的double緩衝區,該緩衝區攜帶先前的double緩衝區內容

下麵是說明duplicate()方法的示例:

示例1:

// Java program to demonstrate 
// duplicate() method 
// Using direct Doublebuffer 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the DoubleBuffer 
        int capacity = 10; 
  
        // Creating the DoubleBuffer 
        try { 
  
            // creating object of Doublebuffer 
            // and allocating size capacity 
            DoubleBuffer db1 = DoubleBuffer.allocate(capacity); 
  
            // putting the value in Doublebuffer 
            db1.put(8.56F); 
            db1.put(2, 9.61F); 
            db1.rewind(); 
  
            // print the Original DoubleBuffer 
            System.out.println("Original DoubleBuffer:  "
                               + Arrays.toString(db1.array())); 
  
            // Creating a duplicate copy of DoubleBuffer 
            // using duplicate() method 
            DoubleBuffer db2 = db1.duplicate(); 
  
            // print the duplicate copy of DoubleBuffer 
            System.out.print("\nDuplicate DoubleBuffer: "
                             + Arrays.toString(db2.array())); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
輸出:
Original DoubleBuffer:  [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

Duplicate DoubleBuffer: [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

示例2:

// Java program to demonstrate 
// duplicate() method 
// using read-onlyDoublebuffer 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the DoubleBuffer 
        int capacity = 10; 
  
        // Creating the DoubleBuffer 
        try { 
  
            // creating object of Doublebuffer 
            // and allocating size capacity 
            DoubleBuffer db1 = DoubleBuffer.allocate(capacity); 
  
            // putting the value in Doublebuffer 
            db1.put(8.56F); 
            db1.put(2, 9.61F); 
            db1.rewind(); 
  
            // print the Original DoubleBuffer 
            System.out.println("Original DoubleBuffer:  "
                               + Arrays.toString(db1.array())); 
  
            // Creating a read-only copy of DoubleBuffer 
            // using asReadOnlyBuffer() method 
            DoubleBuffer readonly = db1.asReadOnlyBuffer(); 
  
            // print the read-only copy of DoubleBuffer 
            System.out.print("\nread-only DoubleBuffer:  "); 
            while (readonly.hasRemaining()) 
                System.out.print(readonly.get() + ", "); 
            System.out.println(""); 
  
            // Rewinding the readonly DoubleBuffer 
            readonly.rewind(); 
  
            // Creating a duplicate copy of DoubleBuffer 
            // using duplicate() method 
            DoubleBuffer db2 = readonly.duplicate(); 
  
            // print the duplicate copy of DoubleBuffer 
            System.out.print("\nduplicate copy of read-only DoubleBuffer:  "); 
  
            while (db2.hasRemaining()) 
                System.out.print(db2.get() + ", "); 
            System.out.println(""); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
輸出:
Original DoubleBuffer:  [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

read-only DoubleBuffer:  8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 

duplicate copy of read-only DoubleBuffer:  8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,


相關用法


注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 DoubleBuffer duplicate() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。