當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java LongBuffer rewind()用法及代碼示例


java.nio.LongBuffer類的rewind()方法用於倒帶此緩衝區。通過倒帶此Buffer,可以執行以下操作:

  • 當前位置設置為零
  • 如果有標記,則將其丟棄,但標記值不變。

用法:

public LongBuffer 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 LongBuffer 
        // using allocate() method 
        LongBuffer longBuffer = LongBuffer.allocate(4); 
  
        // put long value in longBuffer 
        // using put() method 
        longBuffer.put(10); 
        longBuffer.put(20); 
  
        // print the long buffer 
        System.out.println( 
            "Buffer before operation: "
            + Arrays.toString( 
                  longBuffer.array()) 
            + "\nPosition: "
            + longBuffer.position() 
            + "\nLimit: "
            + longBuffer.limit()); 
  
        // rewind the Buffer 
        // using rewind() method 
        longBuffer.rewind(); 
  
        // print the longbuffer 
        System.out.println( 
            "\nBuffer after operation: "
            + Arrays.toString( 
                  longBuffer.array()) 
            + "\nPosition: "
            + longBuffer.position() 
            + "\nLimit: "
            + longBuffer.limit()); 
    } 
}
輸出:
Buffer before operation: [10, 20, 0, 0]
Position: 2
Limit: 4

Buffer after operation: [10, 20, 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 LongBuffer 
        // using allocate() method 
        LongBuffer longBuffer 
            = LongBuffer.allocate(5); 
  
        // put long value in longBuffer 
        // using put() method 
        longBuffer.put(10); 
        longBuffer.put(20); 
        longBuffer.put(30); 
  
        // mark will be going to 
        // discarded by rewind() 
        longBuffer.mark(); 
  
        // print the buffer 
        System.out.println( 
            "Buffer before operation: "
            + Arrays.toString( 
                  longBuffer.array()) 
            + "\nPosition: "
            + longBuffer.position() 
            + "\nLimit: "
            + longBuffer.limit()); 
  
        // Rewind the Buffer 
        // using rewind() method 
        longBuffer.rewind(); 
  
        // print the buffer 
        System.out.println( 
            "\nBuffer after operation: "
            + Arrays.toString( 
                  longBuffer.array()) 
            + "\nPosition: "
            + longBuffer.position() 
            + "\nLimit: "
            + longBuffer.limit()); 
    } 
}
輸出:
Buffer before operation: [10, 20, 30, 0, 0]
Position: 3
Limit: 5

Buffer after operation: [10, 20, 30, 0, 0]
Position: 0
Limit: 5

參考: https://docs.oracle.com/javase/9/docs/api/java/nio/LongBuffer.html#mark–



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 LongBuffer rewind() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。