java.nio.FloatBuffer类的hasArray()方法用于确保给定缓冲区是否由可访问的float数组支持。如果此缓冲区有可访问的后备数组,则返回true,否则返回false。如果此方法返回true,则可以安全地调用array()和arrayOffset()方法,因为它们在支持数组上起作用。
用法:
public final 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 FloatBuffer
int capacity = 10;
// Creating the FloatBuffer
try {
// creating object of floatbuffer
// and allocating size capacity
FloatBuffer fb = FloatBuffer.allocate(capacity);
// putting the value in floatbuffer
fb.put(8.56F);
fb.put(2, 9.61F);
fb.rewind();
// checking FloatBuffer fb is backed by array or not
boolean isArray = fb.hasArray();
// checking if else condition
if (isArray)
System.out.println("FloatBuffer fb is"
+ " backed by array");
else
System.out.println("FloatBuffer fb is"
+ " not backed by any array");
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
输出:
FloatBuffer fb 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 FloatBuffer
int capacity = 10;
// Creating the FloatBuffer
try {
// creating object of floatbuffer
// and allocating size capacity
FloatBuffer fb = FloatBuffer.allocate(capacity);
// putting the value in floatbuffer
fb.put(8.56F);
fb.put(2, 9.61F);
fb.rewind();
// Creating a read-only copy of FloatBuffer
// using asReadOnlyBuffer() method
FloatBuffer fb1 = fb.asReadOnlyBuffer();
// checking FloatBuffer fb is backed by array or not
boolean isArray = fb1.hasArray();
// checking if else condition
if (isArray)
System.out.println("FloatBuffer fb is"
+ " backed by array");
else
System.out.println("FloatBuffer fb is"
+ " not backed by any array");
}
catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException catched");
}
catch (ReadOnlyBufferException e) {
System.out.println("ReadOnlyBufferException catched");
}
}
}
输出:
FloatBuffer fb is not backed by any array
相关用法
- Java ByteBuffer hasArray()用法及代码示例
- Java DoubleBuffer hasArray()用法及代码示例
- Java ShortBuffer hasArray()用法及代码示例
- Java FloatBuffer duplicate()用法及代码示例
- Java FloatBuffer compact()用法及代码示例
- Java FloatBuffer asReadOnlyBuffer()用法及代码示例
- Java FloatBuffer compareTo()用法及代码示例
- Java FloatBuffer arrayOffset()用法及代码示例
- Java FloatBuffer allocate()用法及代码示例
- Java FloatBuffer array()用法及代码示例
- Java FloatBuffer limit()用法及代码示例
- Java FloatBuffer equals()用法及代码示例
- Java FloatBuffer slice()用法及代码示例
- Java FloatBuffer wrap()用法及代码示例
- Java Buffer hasArray()用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 FloatBuffer hasArray() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。