当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java DoubleBuffer array()用法及代码示例


java.nio.DoubleBuffer类的array()方法用于返回支持此缓冲区的双精度数组。对该缓冲区内容的修改将导致返回数组的内容被修改,反之亦然。 Invoke()在调用此方法之前,先使用hasArray()方法,以确保此缓冲区具有可访问的后备数组

用法:

public final float[] array()

返回值:此方法返回支持此缓冲区的数组。


异常:如果此缓冲区由数组支持但为只读,则此方法将引发ReadOnlyBufferException。

以下示例程序旨在说明array()方法:

范例1:

// Java program to demonstrate 
// array() 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 db = DoubleBuffer.allocate(capacity); 
  
            // putting the value in Doublebuffer 
            db.put(8.56F); 
            db.put(2, 9.61F); 
            db.rewind(); 
  
            // getting array from db DoubleBuffer using array() method 
            double[] dbb = db.array(); 
  
            // printing the DoubleBuffer db 
            System.out.println("DoubleBuffer:  "
                               + Arrays.toString(dbb)); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("IllegalArgumentException catched"); 
        } 
  
        catch (ReadOnlyBufferException e) { 
  
            System.out.println("ReadOnlyBufferException catched"); 
        } 
    } 
}
输出:
DoubleBuffer:  [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

示例2:

// Java program to demonstrate 
// array() method 
  
import java.nio.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // Declaring the capacity of the db 
        int capacity1 = 10; 
  
        // Declaring the capacity of the db1 
        int capacity2 = 5; 
  
        // Creating the DoubleBuffer 
        try { 
            // 
            // db 
            // 
            // creating object of Doublebuffer db 
            // and allocating size capacity 
            DoubleBuffer db = DoubleBuffer.allocate(capacity1); 
  
            // putting the value in db 
            db.put(9.56F); 
            db.put(2, 7.61F); 
            db.put(3, 4.61F); 
            db.rewind(); 
  
            // print the DoubleBuffer 
            System.out.println("DoubleBuffer db: "
                               + Arrays.toString(db.array())); 
  
            // 
            // db1 
            // 
            // creating object of Doublebuffer db1 
            // and allocating size capacity 
            DoubleBuffer db1 = DoubleBuffer.allocate(capacity2); 
  
            // putting the value in db1 
            db1.put(1, 4.56F); 
            db1.put(2, 6.45F); 
            db1.rewind(); 
  
            // print the DoubleBuffer 
            System.out.println("\nDoubleBuffer db1:  "
                               + Arrays.toString(db1.array())); 
  
            // Creating a read-only copy of DoubleBuffer 
            // using asReadOnlyBuffer() method 
            DoubleBuffer readOnlyDb = db.asReadOnlyBuffer(); 
  
            // print the DoubleBuffer 
            System.out.print("\nReadOnlyBuffer DoubleBuffer: "); 
  
            while (readOnlyDb.hasRemaining()) 
                System.out.print(readOnlyDb.get() + ", "); 
  
            // try to change readOnlyDb 
            System.out.println("\n\nTrying to get the array"
                               + " from ReadOnlyDb for editing"); 
            Arrays.toString(readOnlyDb.array()); 
        } 
        catch (IllegalArgumentException e) { 
            System.out.println("IllegalArgumentException catched"); 
        } 
        catch (ReadOnlyBufferException e) { 
            System.out.println("Exception thrown: " + e); 
        } 
    } 
}
输出:
DoubleBuffer db: [9.5600004196167, 0.0, 7.610000133514404, 4.610000133514404, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

DoubleBuffer db1:  [0.0, 4.559999942779541, 6.449999809265137, 0.0, 0.0]

ReadOnlyBuffer DoubleBuffer: 9.5600004196167, 0.0, 7.610000133514404, 4.610000133514404, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 

Trying to get the array from ReadOnlyDb for editing
Exception thrown: java.nio.ReadOnlyBufferException


相关用法


注:本文由纯净天空筛选整理自pawan_asipu大神的英文原创作品 DoubleBuffer array() method in Java With Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。