Java CharBuffer hasArray()方法

Java CharBuffer hasArray()方法

java.nio.CharBuffer 类的 hasArray() 方法用于确保给定的缓冲区是否有一个可访问的char数组作为支撑。如果这个缓冲区有一个可访问的支持数组,它就返回真,否则就返回假。如果该方法返回true,那么可以安全地调用array()和arrayOffset()方法,因为它们对支持数组起作用。

语法

public final boolean hasArray()

返回值: 当且仅当这个缓冲区由一个数组支持并且不是只读时,该方法将返回 true 。否则它将返回 false

下面是说明 hasArray() 方法的例子。

例子1: 当缓冲区由一个数组支持时

// Java program to demonstrate
// hasArray() 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 Intbuffer
            // and allocating size capacity
            CharBuffer cb = CharBuffer.allocate(capacity);
  
            // putting the value in Intbuffer
            cb.put('a');
            cb.put(2, 'b');
            cb.rewind();
  
            // checking CharBuffer cb is backed by array or not
            boolean isArray = cb.hasArray();
  
            // checking if else condition
            if (isArray)
                System.out.println("CharBuffer cb is"
                                   + " backed by array");
            else
                System.out.println("CharBuffer cb is"
                                   + " not backed by any array");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}

输出:

CharBuffer cb is backed by array

例子2: 当缓冲区没有数组支持的时候

// Java program to demonstrate
// hasArray() 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();
  
            // Creating a read-only copy of CharBuffer
            // using asReadOnlyBuffer() method
            CharBuffer cb1 = cb.asReadOnlyBuffer();
  
            // checking CharBuffer cb is backed by array or not
            boolean isArray = cb1.hasArray();
  
            // checking if else condition
            if (isArray)
                System.out.println("CharBuffer cb is"
                                   + " backed by array");
            else
                System.out.println("CharBuffer cb is"
                                   + " not backed by any array");
        }
  
        catch (IllegalArgumentException e) {
            System.out.println("IllegalArgumentException catched");
        }
  
        catch (ReadOnlyBufferException e) {
            System.out.println("ReadOnlyBufferException catched");
        }
    }
}

输出:

CharBuffer cb is not backed by any array

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南