CharBuffer array() method in Java

The array() method of java.nio.CharBuffer Class is used to return the char array that backs this buffer. Any modifications done to this buffer’s content will cause the returned array’s content also to be modified, and vice versa. hasArray() method is invoked before invoking this method in order to ensure that this buffer has an accessible backing array.

Syntax:

public final char[] array()

Return Value: This method returns the array that backs this buffer.



Throws: This method throws the ReadOnlyBufferException(If this buffer is backed by an array but is read-only)

Below program illustrates the array() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
        }
    }
}

chevron_right


Output:

CharBuffer: [a, , b, , , , , , , ]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.