java.nio.LongBuffer類的limit()方法用於修改此LongBuffer的限製。此方法將要設置的限製作為參數,並將其設置為此Buffer的新限製。如果此緩衝區的標記已經定義並且大於新指定的限製,則不會設置並丟棄此新限製。
用法:
public final LongBuffer limit(int newLimit)
參數:該方法采用整數類型的一個參數newLimit,該參數表示要設置為緩衝區的新限製的限製。
返回值:在將指定的新限製設置為該緩衝區的新限製之後,此方法將返回此緩衝區。
下麵是說明limit()方法的示例:
範例1:
// Java program to demonstrate
// limit() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// defining and allocating LongBuffer
// using allocate() method
LongBuffer longBuffer
= LongBuffer.allocate(4);
// put long value in longBuffer
// using put() method
longBuffer.put(20);
longBuffer.put(30);
// print the long buffer
System.out.println(
"LongBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
longBuffer.array())
+ "\nPosition: "
+ longBuffer.position()
+ "\nLimit: "
+ longBuffer.limit());
// Limit the longBuffer
// using limit() method
longBuffer.limit(1);
// print the long buffer
System.out.println(
"\nLongBuffer after "
+ "setting buffer's limit: "
+ Arrays.toString(
longBuffer.array())
+ "\nPosition: "
+ longBuffer.position()
+ "\nLimit: "
+ longBuffer.limit());
}
}
輸出:
LongBuffer before setting buffer's limit: [20, 30, 0, 0] Position: 2 Limit: 4 LongBuffer after setting buffer's limit: [20, 30, 0, 0] Position: 1 Limit: 1
範例2:
// Java program to demonstrate
// limit() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// defining and allocating LongBuffer
// using allocate() method
LongBuffer longBuffer
= LongBuffer.allocate(5);
// put double value in LongBuffer
// using put() method
longBuffer.put(20);
longBuffer.put(30);
longBuffer.put(40);
// mark will be going to
// discarded by limit()
longBuffer.mark();
// print the long buffer
System.out.println(
"LongBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
longBuffer.array())
+ "\nPosition: "
+ longBuffer.position()
+ "\nLimit: "
+ longBuffer.limit());
// Limit the longBuffer
// using limit() method
longBuffer.limit(4);
// print the long buffer
System.out.println(
"\nLongBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
longBuffer.array())
+ "\nPosition: "
+ longBuffer.position()
+ "\nLimit: "
+ longBuffer.limit());
}
}
輸出:
LongBuffer before setting buffer's limit: [20, 30, 40, 0, 0] Position: 3 Limit: 5 LongBuffer before setting buffer's limit: [20, 30, 40, 0, 0] Position: 3 Limit: 4
參考:https://docs.oracle.com/javase/9/docs/api/java/nio/LongBuffer.html#rewind–
相關用法
- Java LongBuffer flip()用法及代碼示例
- Java LongBuffer clear()用法及代碼示例
- Java LongBuffer reset()用法及代碼示例
- Java LongBuffer mark()用法及代碼示例
- Java LongBuffer rewind()用法及代碼示例
- Java ShortBuffer limit()用法及代碼示例
- Java FloatBuffer limit()用法及代碼示例
- Java IntBuffer limit()用法及代碼示例
- Java LongBuffer wrap()用法及代碼示例
- Java LongBuffer asReadOnlyBuffer()用法及代碼示例
- Java LongBuffer slice()用法及代碼示例
- Java LongBuffer equals()用法及代碼示例
- Java LongBuffer compact()用法及代碼示例
- Java LongBuffer duplicate()用法及代碼示例
- Java LongBuffer allocate()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 LongBuffer limit() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。