📜  Java中的 LongBuffer rewind() 方法及示例

📅  最后修改于: 2022-05-13 01:55:49.318000             🧑  作者: Mango

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–