java.nio.FloatBuffer類的limit()方法用於修改此FloatBuffer的限製。此方法將要設置的限製作為參數,並將其設置為此Buffer的新限製。如果此緩衝區的標記已經定義並且大於新指定的限製,則不會設置並丟棄此新限製。
用法:
public final FloatBuffer 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 FloatBuffer
// using allocate() method
FloatBuffer floatBuffer
= FloatBuffer.allocate(4);
// put float value in FloatBuffer
// using put() method
floatBuffer.put(20.5f);
floatBuffer.put(30.5f);
// print the float buffer
System.out.println(
"FloatBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition: "
+ floatBuffer.position()
+ "\nLimit: "
+ floatBuffer.limit());
// Limit the floatBuffer
// using limit() method
floatBuffer.limit(1);
// print the float buffer
System.out.println(
"\nFloatBuffer after "
+ "setting buffer's limit: "
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition: "
+ floatBuffer.position()
+ "\nLimit: "
+ floatBuffer.limit());
}
}
FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 0.0, 0.0]
Position: 2
Limit: 4FloatBuffer after setting buffer’s limit:
[20.5, 30.5, 0.0, 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 FloatBuffer
// using allocate() method
FloatBuffer floatBuffer
= FloatBuffer.allocate(5);
// put float value in FloatBuffer
// using put() method
floatBuffer.put(20.5f);
floatBuffer.put(30.5f);
floatBuffer.put(40.5f);
// mark will be going to
// discarded by limit()
floatBuffer.mark();
// print the float buffer
System.out.println(
"FloatBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition: "
+ floatBuffer.position()
+ "\nLimit: "
+ floatBuffer.limit());
// Limit the floatBuffer
// using limit() method
floatBuffer.limit(4);
// print the double buffer
System.out.println(
"\nFloatBuffer before "
+ "setting buffer's limit: "
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition: "
+ floatBuffer.position()
+ "\nLimit: "
+ floatBuffer.limit());
}
}
FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 40.5, 0.0, 0.0]
Position: 3
Limit: 5FloatBuffer before setting buffer’s limit:
[20.5, 30.5, 40.5, 0.0, 0.0]
Position: 3
Limit: 4
參考:https://docs.oracle.com/javase/9/docs/api/java/nio/FloatBuffer.html#limit-int-
相關用法
- Java FloatBuffer arrayOffset()用法及代碼示例
- Java FloatBuffer asReadOnlyBuffer()用法及代碼示例
- Java FloatBuffer duplicate()用法及代碼示例
- Java FloatBuffer allocate()用法及代碼示例
- Java FloatBuffer hasArray()用法及代碼示例
- Java FloatBuffer compareTo()用法及代碼示例
- Java FloatBuffer compact()用法及代碼示例
- Java FloatBuffer wrap()用法及代碼示例
- Java FloatBuffer slice()用法及代碼示例
- Java FloatBuffer array()用法及代碼示例
- Java FloatBuffer equals()用法及代碼示例
- Java LongBuffer limit()用法及代碼示例
- Java IntBuffer limit()用法及代碼示例
- Java ShortBuffer limit()用法及代碼示例
- Java FloatBuffer get()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 FloatBuffer limit() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。