Java中的IntBuffer hasArray()方法

通过使用hasArray()类java.nio.IntBuffer中的方法,可以检查缓冲区是否具有可访问的int数组的支持。如果缓冲区具有可访问的int数组的支持,则此方法返回true,否则返回false。

演示此的程序如下所示-

示例

import java.nio.*;
import java.util.*;
public class Demo {
   public static void main(String[] args) {
      int n = 5;
      try {
         IntBuffer buffer = IntBuffer.allocate(5);
         buffer.put(8);
         buffer.put(1);
         buffer.put(3);
         buffer.put(7);
         buffer.put(5);
         buffer.rewind();
         System.out.println("The IntBuffer is: " + Arrays.toString(buffer.array()));
         boolean flag = buffer.hasArray();
         if (flag)
            System.out.println("The IntBuffer is backed by an array");
         else
            System.out.println("The IntBuffer is not backed by any array");
      } catch (IllegalArgumentException e) {
         System.out.println("Error!!! IllegalArgumentException");
      } catch (ReadOnlyBufferException e){
         System.out.println("Error!!! ReadOnlyBufferException");
      }
   }
}

上面程序的输出如下-

输出结果

The IntBuffer is: [8, 1, 3, 7, 5]
The IntBuffer is backed by an array