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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。