java.nio.FloatBuffer類的rewind()方法用於倒帶此緩衝區。此方法將位置設置為零,並且限製不受影響,並且如果有任何先前標記的位置將被丟棄。當需要對channel-write進行序列化或進行get操作時,應調用此方法。這意味著,如果已經寫入了緩衝區數據,則需要將其複製到另一個數組中。例如:
out.write(buf); // Writes the remaining data buf.rewind(); // Rewind the buffer buf.get(array); // Copy data into array
用法:
public final FloatBuffer rewind()
參數:該方法不帶任何參數。
返回值:該方法返回此緩衝區。
下麵是說明rewind()方法的示例:
範例1:
// Java program to demonstrate
// rewind() 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 char value in FloatBuffer
// using put() method
floatBuffer.put(10.5f);
floatBuffer.put(20.5f);
// print the float buffer
System.out.println("Buffer before operation:"
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition:"
+ floatBuffer.position()
+ "\nLimit:"
+ floatBuffer.limit());
// rewind the Buffer
// using rewind() method
floatBuffer.rewind();
// print the floatbuffer
System.out.println("\nBuffer after operation:"
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition:"
+ floatBuffer.position()
+ "\nLimit:"
+ floatBuffer.limit());
}
}
輸出:
Buffer before operation:[10.5, 20.5, 0.0, 0.0] Position:2 Limit:4 Buffer after operation:[10.5, 20.5, 0.0, 0.0] Position:0 Limit:4
範例2:
// Java program to demonstrate
// rewind() 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(10.5f);
floatBuffer.put(20.5f);
floatBuffer.put(30.5f);
// mark will be going to discarded by rewind()
floatBuffer.mark();
// print the buffer
System.out.println("Buffer before operation:"
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition:"
+ floatBuffer.position()
+ "\nLimit:"
+ floatBuffer.limit());
// Rewind the Buffer
// using rewind() method
floatBuffer.rewind();
// print the buffer
System.out.println("\nBuffer after operation:"
+ Arrays.toString(
floatBuffer.array())
+ "\nPosition:"
+ floatBuffer.position()
+ "\nLimit:"
+ floatBuffer.limit());
}
}
輸出:
Buffer before operation:[10.5, 20.5, 30.5, 0.0, 0.0] Position:3 Limit:5 Buffer after operation:[10.5, 20.5, 30.5, 0.0, 0.0] Position:0 Limit:5
參考: https://docs.oracle.com/javase/9/docs/api/java/nio/FloatBuffer.html#rewind-
相關用法
- Java FloatBuffer get()用法及代碼示例
- Java FloatBuffer put()用法及代碼示例
- Java FloatBuffer reset()用法及代碼示例
- Java FloatBuffer clear()用法及代碼示例
- Java CharBuffer rewind()用法及代碼示例
- Java FloatBuffer mark()用法及代碼示例
- Java Buffer rewind()用法及代碼示例
- Java FloatBuffer flip()用法及代碼示例
- Java ByteBuffer rewind()用法及代碼示例
- Java FloatBuffer hasArray()用法及代碼示例
- Java FloatBuffer arrayOffset()用法及代碼示例
- Java FloatBuffer compareTo()用法及代碼示例
- Java FloatBuffer compact()用法及代碼示例
- Java FloatBuffer allocate()用法及代碼示例
- Java FloatBuffer limit()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 FloatBuffer rewind() methods in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。