Java 中 IntBuffer 的 hasArray() 方法
可以透過使用 Java.nio.IntBuffer 類的 hasArray() 方法檢查緩衝區是否支援可訪問的 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
廣告