DoubleBuffer的Java中hasArray()方法
可以通过使用java.nio.DoubleBuffer类中的hasArray()方法,来判断缓冲区是否带有可访问的双精度数组。如果缓冲区带有可访问的双精度数组则返回true,否则返回false。
下面给出一个对此进行演示的程序 −
示例
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { DoubleBuffer buffer = DoubleBuffer.allocate(5); buffer.put(4.5D); buffer.put(1.2D); buffer.put(3.9D); buffer.put(7.5D); buffer.put(5.8D); buffer.rewind(); System.out.println("The DoubleBuffer is: " + Arrays.toString(buffer.array())); boolean flag = buffer.hasArray(); if (flag) System.out.println("The DoubleBuffer is backed by an array"); else System.out.println("The DoubleBuffer is not backed by any array"); } catch (IllegalArgumentException e) { System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } } }
上方程序的输出如下 −
输出
The DoubleBuffer is: [4.5, 1.2, 3.9, 7.5, 5.8] The DoubleBuffer is backed by an array
广告