java.nio.DoubleBuffer類的allocate()方法用於在現有緩衝區旁邊分配一個新的雙緩衝區。新緩衝區的位置將為零。它的極限將是它的容量。其標記將是不確定的。並且其每個元素都將初始化為零。它將有一個支持數組,其數組偏移量將為零。
用法:
public static DoubleBuffer allocate(int capacity)
參數:此方法將新緩衝區的容量增加一倍,作為參數。
返回值:此方法返回新的雙緩衝區。
異常:如果容量為負整數,則此方法引發IllegalArgumentException。
以下示例程序旨在說明allocate()方法:
範例1:
// Java program to demonstrate
// allocate() 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
// 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);
System.out.println("DoubleBuffer: "
+ Arrays.toString(db.array()));
}
}
輸出:
DoubleBuffer: [8.5600004196167, 0.0, 9.609999656677246, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
範例2:演示IllegalArgumentException
// Java program to demonstrate
// allocate() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the DoubleBuffer
// by negetive integer
int capacity = -10;
// Creating the DoubleBuffer
try {
// creating object of Doublebuffer
// and allocating size with negetive integer
System.out.println("Trying to allocate a negetive integer");
DoubleBuffer db = DoubleBuffer.allocate(capacity);
}
catch (IllegalArgumentException e) {
System.out.println("Exception thrown: " + e);
}
}
}
輸出:
Trying to allocate a negetive integer Exception thrown: java.lang.IllegalArgumentException
相關用法
- Java DoubleBuffer array()用法及代碼示例
- Java DoubleBuffer equals()用法及代碼示例
- Java DoubleBuffer asReadOnlyBuffer()用法及代碼示例
- Java DoubleBuffer duplicate()用法及代碼示例
- Java DoubleBuffer compact()用法及代碼示例
- Java DoubleBuffer hasArray()用法及代碼示例
- Java DoubleBuffer slice()用法及代碼示例
- Java DoubleBuffer wrap()用法及代碼示例
- Java DoubleBuffer compareTo()用法及代碼示例
- Java DoubleBuffer rewind()用法及代碼示例
- Java DoubleBuffer arrayOffset()用法及代碼示例
- Java FloatBuffer allocate()用法及代碼示例
- Java ByteBuffer allocate()用法及代碼示例
- Java CharBuffer allocate()用法及代碼示例
- Java ShortBuffer allocate()用法及代碼示例
注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 DoubleBuffer allocate() method in Java With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。