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


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


java.nio.CharBuffer類的array()方法用於返回支持此緩衝區的char數組。對此緩衝區的內容所做的任何修改都將導致返回數組的內容也被修改,反之亦然。為了確保此緩衝區具有可訪問的後備數組,在調用此方法之前,將調用hasArray()方法。

用法:

public final char[] array()

返回值:此方法返回支持此緩衝區的數組。


拋出:此方法拋出ReadOnlyBufferException(如果此緩衝區由數組支持,但為隻讀)

以下示例程序旨在說明array()方法:

示例1:

// Java program to demonstrate 
// array() method 
  
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 CharBuffer 
            // and allocating size capacity 
            CharBuffer cb = CharBuffer.allocate(capacity); 
  
            // putting the value in CharBuffer 
            cb.put('a'); 
            cb.put(2, 'b'); 
            cb.rewind(); 
  
            // getting array from cb 
            // CharBuffer using array() method 
            char[] cbb = cb.array(); 
  
            // printing the CharBuffer cb 
            System.out.println("CharBuffer: "
                               + Arrays.toString(cbb)); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
輸出:
CharBuffer: [a, , b, , , , , , , ]

示例2:

// Java program to demonstrate 
// array() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the cb 
        int capacity1 = 10; 
  
        // Declaring the capacity of the cb1 
        int capacity2 = 5; 
  
        // Creating the CharBuffer 
        try { 
            // 
            // cb 
            // 
            // creating object of CharBuffer cb 
            // and allocating size capacity 
            CharBuffer cb = CharBuffer.allocate(capacity1); 
  
            // putting the value in cb 
            cb.put('a'); 
            cb.put(2, 'b'); 
            cb.put(3, 'c'); 
            cb.rewind(); 
  
            // print the CharBuffer 
            System.out.println("CharBuffer cb: "
                               + Arrays.toString(cb.array())); 
  
            // 
            // cb1 
            // 
            // creating object of CharBuffer cb1 
            // and allocating size capacity 
            CharBuffer cb1 = CharBuffer.allocate(capacity2); 
  
            // putting the value in cb1 
            cb1.put(1, 'd'); 
            cb1.put(2, 'e'); 
            cb1.rewind(); 
  
            // print the CharBuffer 
            System.out.println("\nCharBuffer cb1: "
                               + Arrays.toString(cb1.array())); 
  
            // Creating a read-only copy of CharBuffer 
            // using asReadOnlyBuffer() method 
            CharBuffer readOnlycb = cb.asReadOnlyBuffer(); 
  
            // print the CharBuffer 
            System.out.print("\nReadOnlyBuffer CharBuffer: "); 
  
            while (readOnlycb.hasRemaining()) 
                System.out.print(readOnlycb.get() + ", "); 
  
            // try to change readOnlycb 
            System.out.println("\n\nTrying to get the array"
                               + " from ReadOnlycb for editing"); 
  
            char[] cbarr = readOnlycb.array(); 
        } 
        catch (IllegalArgumentException e) { 
            System.out.println("IllegalArgumentException catched"); 
        } 
        catch (ReadOnlyBufferException e) { 
            System.out.println("Exception thrown: " + e); 
        } 
    } 
}
輸出:
CharBuffer cb: [a,, b, c,,,,,, ]

CharBuffer cb1: [, d, e,, ]

ReadOnlyBuffer CharBuffer: a,, b, c,,,,,,, 

Trying to get the array from ReadOnlycb for editing
Exception thrown: java.nio.ReadOnlyBufferException


相關用法


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