當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java DoubleBuffer hasArray()用法及代碼示例


java.nio.DoubleBuffer類的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 DoubleBuffer 
        int capacity = 10; 
  
        // Creating the DoubleBuffer 
        try { 
  
            // creating object of Doublebuffer 
            // and allocating size capacity 
            DoubleBuffer fb = DoubleBuffer.allocate(capacity); 
  
            // putting the value in Doublebuffer 
            fb.put(8.56F); 
            fb.put(2, 9.61F); 
            fb.rewind(); 
  
            // checking DoubleBuffer fb is backed by array or not 
            boolean isArray = fb.hasArray(); 
  
            // checking if else condition 
            if (isArray) 
                System.out.println("DoubleBuffer fb is"
                                   + " backed by array"); 
            else
                System.out.println("DoubleBuffer fb is"
                                   + " not backed by any array"); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
輸出:
DoubleBuffer 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 DoubleBuffer 
        int capacity = 10; 
  
        // Creating the DoubleBuffer 
        try { 
  
            // creating object of Doublebuffer 
            // and allocating size capacity 
            DoubleBuffer fb = DoubleBuffer.allocate(capacity); 
  
            // putting the value in Doublebuffer 
            fb.put(8.56F); 
            fb.put(2, 9.61F); 
            fb.rewind(); 
  
            // Creating a read-only copy of DoubleBuffer 
            // using asReadOnlyBuffer() method 
            DoubleBuffer fb1 = fb.asReadOnlyBuffer(); 
  
            // checking DoubleBuffer fb is backed by array or not 
            boolean isArray = fb1.hasArray(); 
  
            // checking if else condition 
            if (isArray) 
                System.out.println("DoubleBuffer fb is"
                                   + " backed by array"); 
            else
                System.out.println("DoubleBuffer fb is"
                                   + " not backed by any array"); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
輸出:
DoubleBuffer fb is not backed by any array


相關用法


注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 DoubleBuffer hasArray() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。