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
相關用法
- Java DoubleBuffer rewind()用法及代碼示例
- Java DoubleBuffer equals()用法及代碼示例
- Java DoubleBuffer duplicate()用法及代碼示例
- Java DoubleBuffer allocate()用法及代碼示例
- Java DoubleBuffer compact()用法及代碼示例
- Java DoubleBuffer hasArray()用法及代碼示例
- Java DoubleBuffer compareTo()用法及代碼示例
- Java DoubleBuffer slice()用法及代碼示例
- Java DoubleBuffer wrap()用法及代碼示例
- Java DoubleBuffer asReadOnlyBuffer()用法及代碼示例
- Java DoubleBuffer arrayOffset()用法及代碼示例
- Java DoubleBuffer get()用法及代碼示例
- Java DoubleBuffer put()方法用法及代碼示例
- Java DoubleBuffer mark()用法及代碼示例
- Java DoubleBuffer reset()用法及代碼示例
注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 DoubleBuffer array() method in Java With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。