java.nio.Buffer類的hasArray()方法用於告訴此緩衝區是否由可訪問數組支持。如果此方法返回true,則可以安全地調用array和arrayOffset()方法。
用法:
public abstract 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 ByteBuffer
int capacity = 10;
// Creating the ByteBuffer
try {
// creating object of bytebuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the value in bytebuffer
bb.put((byte)10);
bb.put((byte)20);
bb.rewind();
// Typecast bytebuffer to Buffer
Buffer buffer = (Buffer)bb;
// checking buffer is backed by array or not
boolean isArray = buffer.hasArray();
// checking if else condition
if (isArray)
System.out.println("buffer is"
+ " backed by array");
else
System.out.println("buffer is"
+ " not backed by any array");
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
輸出:
buffer 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 ByteBuffer
int capacity = 10;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the value in ByteBuffer
bb.put((byte)8.56F);
bb.put((byte)10);
bb.rewind();
// Creating a read-only copy of ByteBuffer
// using asReadOnlyBuffer() method
ByteBuffer bb1 = bb.asReadOnlyBuffer();
// Typecast read-only ByteBuffer to read-only buffer
Buffer buffer = (Buffer)bb1;
// checking Buffer buffer is backed by array or not
boolean isArray = buffer.hasArray();
// checking if else condition
if (isArray)
System.out.println("buffer is"
+ " backed by array");
else
System.out.println("buffer is"
+ " not backed by any array");
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
輸出:
buffer is not backed by any array
參考: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#hasArray-
相關用法
- Java Buffer limit()用法及代碼示例
- Java Buffer isDirect()用法及代碼示例
- Java Buffer clear()用法及代碼示例
- Java Buffer hasRemaining()用法及代碼示例
- Java Buffer position()用法及代碼示例
- Java Buffer isReadOnly()用法及代碼示例
- Java Buffer mark()用法及代碼示例
- Java Buffer array()用法及代碼示例
- Java Buffer rewind()用法及代碼示例
- Java Buffer reset()用法及代碼示例
- Java Buffer flip()用法及代碼示例
- Java Buffer remaining()用法及代碼示例
- Java ShortBuffer hasArray()用法及代碼示例
- Java DoubleBuffer hasArray()用法及代碼示例
- Java ByteBuffer hasArray()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Buffer hasArray() methods in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。