java.nio.LongBuffer類的allocate()方法用於在現有緩衝區旁邊分配一個新的Long緩衝區。新緩衝區的位置將為零。它的極限將是它的容量。其標記將是不確定的。並且其每個元素都將初始化為零。它將有一個支持數組,其數組偏移量將為零。
用法:
public static LongBuffer allocate(Long capacity)
參數:此方法將新緩衝區的容量Long用作參數。
返回值:此方法返回新的Long緩衝區。
異常:如果容量為負Longeger,則此方法將拋出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 LongBuffer
int Capacity = 10;
// Creating the LongBuffer
// creating object of Longbuffer
// and allocating size capacity
LongBuffer ib = LongBuffer.allocate(Capacity);
// putting the value in Longbuffer
ib.put(11);
ib.put(2, 19);
System.out.println("LongBuffer: "
+ Arrays.toString(ib.array()));
}
}
輸出:
LongBuffer: [11, 0, 19, 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 LongBuffer
// by negative Longeger
int Capacity = -10;
// Creating the LongBuffer
try {
// creating object of Longbuffer
// and allocating size with negative Longger
System.out.println("Trying to allocate a Negative Longeger");
LongBuffer ib = LongBuffer.allocate(Capacity);
}
catch (IllegalArgumentException e) {
System.out.println("Exception thrown: " + e);
}
}
}
輸出:
Trying to allocate a Negative Longeger Exception thrown: java.lang.IllegalArgumentException
相關用法
- Java LongBuffer compact()用法及代碼示例
- Java LongBuffer equals()用法及代碼示例
- Java LongBuffer asReadOnlyBuffer()用法及代碼示例
- Java LongBuffer wrap()用法及代碼示例
- Java LongBuffer slice()用法及代碼示例
- Java LongBuffer duplicate()用法及代碼示例
- Java LongBuffer hasArray()用法及代碼示例
- Java LongBuffer arrayOffset()用法及代碼示例
- Java LongBuffer limit()用法及代碼示例
- Java LongBuffer reset()用法及代碼示例
- Java LongBuffer flip()用法及代碼示例
- Java LongBuffer clear()用法及代碼示例
- Java LongBuffer rewind()用法及代碼示例
- Java LongBuffer mark()用法及代碼示例
- Java IntBuffer allocate()用法及代碼示例
注:本文由純淨天空篩選整理自pawan_asipu大神的英文原創作品 LongBuffer allocate() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。