Java CharBuffer array()方法

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南