这个 数组() 方法 JAVA尼奥。CharBuffer 类用于返回支持此缓冲区的字符数组。对该缓冲区的内容所做的任何修改都将导致返回数组的内容也被修改,反之亦然。在调用此方法之前调用hasArray()方法,以确保此缓冲区具有可访问的后备数组。
null
语法:
public final char[] array()
返回值: 此方法返回 大堆 这就是缓冲区。
抛出: 这个方法抛出了 ReadOnlyBufferException (如果此缓冲区由数组支持,但为只读)
下面的程序说明了 数组() 方法:
例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( "CharBuffer 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( "ReadOnlyBuffer CharBuffer: " ); while (readOnlycb.hasRemaining()) System.out.print(readOnlycb.get() + ", " ); // try to change readOnlycb System.out.println( "Trying 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
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END