Java 中的 FloatBuffer hasArray() 方法
可以使用 java.nio.FloatBuffer 类中的 hasArray() 方法来检查缓冲区是否具有可访问的浮点数组作为后盾。如果缓冲区具有可访问的浮点数组作为后盾,则此方法返回 true,否则返回 false。
下面给出了演示此方法的程序 −
示例
import java.nio.*; import java.util.*; public class Demo { public static void main(String[] args) { int n = 5; try { FloatBuffer buffer = FloatBuffer.allocate(5); buffer.put(4.5F); buffer.put(1.2F); buffer.put(3.9F); buffer.put(7.5F); buffer.put(5.8F); buffer.rewind(); System.out.println("The FloatBuffer is: " + Arrays.toString(buffer.array())); boolean flag = buffer.hasArray(); if (flag) System.out.println("The FloatBuffer is backed by an array"); else System.out.println("The FloatBuffer is not backed by any array"); } catch (IllegalArgumentException e) { System.out.println("Error!!! IllegalArgumentException"); } catch (ReadOnlyBufferException e) { System.out.println("Error!!! ReadOnlyBufferException"); } } }
上述程序的输出如下 −
输出
The FloatBuffer is: [4.5, 1.2, 3.9, 7.5, 5.8] The FloatBuffer is backed by an array
广告