java.nio.ShortBuffer类的rewind()方法用于倒带此缓冲区。通过倒带此Buffer,可以执行以下操作:
- 当前位置设置为零
 - 如果有标记,则将其丢弃,但标记值不变。
 
用法:
public ShortBuffer 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 ShortBuffer 
        // using allocate() method 
        ShortBuffer shortBuffer 
            = ShortBuffer.allocate(4); 
  
        // put short value in shortBuffer 
        // using put() method 
        shortBuffer.put((short)10); 
        shortBuffer.put((short)20); 
  
        // print the short buffer 
        System.out.println( 
            "Buffer before operation: "
            + Arrays.toString( 
                  shortBuffer.array()) 
            + "\nPosition: "
            + shortBuffer.position() 
            + "\nLimit: "
            + shortBuffer.limit()); 
  
        // rewind the Buffer 
        // using rewind() method 
        shortBuffer.rewind(); 
  
        // print the shortbuffer 
        System.out.println( 
            "\nBuffer after operation: "
            + Arrays.toString( 
                  shortBuffer.array()) 
            + "\nPosition: "
            + shortBuffer.position() 
            + "\nLimit: "
            + shortBuffer.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 ShortBuffer 
        // using allocate() method 
        ShortBuffer shortBuffer 
            = ShortBuffer.allocate(5); 
  
        // put short value in shortBuffer 
        // using put() method 
        shortBuffer.put((short)10); 
        shortBuffer.put((short)20); 
        shortBuffer.put((short)30); 
  
        // mark will be going to discarded by rewind() 
        shortBuffer.mark(); 
  
        // print the buffer 
        System.out.println( 
            "Buffer before operation: "
            + Arrays.toString( 
                  shortBuffer.array()) 
            + "\nPosition: "
            + shortBuffer.position() 
            + "\nLimit: "
            + shortBuffer.limit()); 
  
        // Rewind the Buffer 
        // using rewind() method 
        shortBuffer.rewind(); 
  
        // print the buffer 
        System.out.println( 
            "\nBuffer after operation: "
            + Arrays.toString( 
                  shortBuffer.array()) 
            + "\nPosition: "
            + shortBuffer.position() 
            + "\nLimit: "
            + shortBuffer.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/ShortBuffer.html#rewind–
相关用法
- Java ShortBuffer toString()用法及代码示例
 - Java ShortBuffer mark()用法及代码示例
 - Java ShortBuffer order()用法及代码示例
 - Java ShortBuffer equals()用法及代码示例
 - Java ShortBuffer duplicate()用法及代码示例
 - Java ShortBuffer hasArray()用法及代码示例
 - Java ShortBuffer array()用法及代码示例
 - Java ShortBuffer isDirect()用法及代码示例
 - Java ShortBuffer slice()用法及代码示例
 - Java ShortBuffer compareTo用法及代码示例
 - Java ShortBuffer asReadOnlyBuffer()用法及代码示例
 - Java ShortBuffer put(int, short)用法及代码示例
 - Java ShortBuffer compact()用法及代码示例
 - Java ShortBuffer arrayOffset()用法及代码示例
 - Java ShortBuffer hashCode()用法及代码示例
 
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 ShortBuffer rewind() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
