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


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


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

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

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


用法:

public abstract CharBuffer duplicate()

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

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

示例1:使用直接字符緩衝區

// Java program to demonstrate 
// duplicate() method 
// Using direct intbuffer 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the CharBuffer 
        int capacity = 10; 
  
        // Creating the CharBuffer 
        try { 
  
            // creating object of Intbuffer 
            // and allocating size capacity 
            CharBuffer cb1 = CharBuffer.allocate(capacity); 
  
            // putting the value in Intbuffer 
            cb1.put('a'); 
            cb1.put(2, 'b'); 
            cb1.rewind(); 
  
            // print the Original CharBuffer 
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb1.array())); 
  
            // Creating a duplicate copy of CharBuffer 
            // using duplicate() method 
            CharBuffer cb2 = cb1.duplicate(); 
  
            // print the duplicate copy of CharBuffer 
            System.out.print("Duplicate CharBuffer: "
                             + Arrays.toString(cb2.array())); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
輸出:
Original CharBuffer: [a, , b, , , , , , , ]
Duplicate CharBuffer: [a, , b, , , , , , , ]

示例2:使用read-onlyintbuffer

// Java program to demonstrate 
// duplicate() method 
// using read-onlyIntbuffer 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the CharBuffer 
        int capacity = 10; 
  
        // Creating the CharBuffer 
        try { 
  
            // creating object of Intbuffer 
            // and allocating size capacity 
            CharBuffer cb1 = CharBuffer.allocate(capacity); 
  
            // putting the value in Intbuffer 
            cb1.put('a'); 
            cb1.put(2, 'b'); 
            cb1.rewind(); 
  
            // print the Original CharBuffer 
            System.out.println("Original CharBuffer: "
                               + Arrays.toString(cb1.array())); 
  
            // Creating a read-only copy of CharBuffer 
            // using asReadOnlyBuffer() method 
            CharBuffer readonly = cb1.asReadOnlyBuffer(); 
  
            // print the read-only copy of CharBuffer 
            System.out.print("read-only CharBuffer: "); 
            while (readonly.hasRemaining()) 
                System.out.print(readonly.get() + ", "); 
            System.out.println(""); 
  
            // Rewinding the readonly CharBuffer 
            readonly.rewind(); 
  
            // Creating a duplicate copy of CharBuffer 
            // using duplicate() method 
            CharBuffer cb2 = readonly.duplicate(); 
  
            // print the duplicate copy of CharBuffer 
            System.out.print("duplicate copy of read-only CharBuffer: "); 
  
            while (cb2.hasRemaining()) 
                System.out.print(cb2.get() + ", "); 
            System.out.println(""); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
輸出:
Original CharBuffer: [a, , b, , , , , , , ]
read-only CharBuffer: a, , b, , , , , , , , 
duplicate copy of read-only CharBuffer: a, , b, , , , , , , , 


相關用法


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