📜  Java中的 CharBuffer flip() 方法及示例

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

Java中的 CharBuffer flip() 方法及示例

Java.nio.CharBuffer 类flip()方法用于翻转这个缓冲区。限制设置为当前位置,然后位置设置为零。如果标记已定义,则将其丢弃。在一系列通道读取或放置操作之后,调用此方法以准备一系列通道写入或相关获取操作。

例如:

在将数据从一个地方传输到另一个地方时,此方法通常与 compact() 方法结合使用。

句法:

public final CharBuffer flip()

返回值:此方法返回此缓冲区。

以下是说明翻转()方法的示例:

示例 1:

// Java program to demonstrate
// flip() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declare and initialize the char array
        char[] cb = { 'a', 'b', 'c' };
  
        // wrap the char array into CharBuffer
        // using wrap() method
        CharBuffer charBuffe
            r
            = CharBuffer.wrap(cb);
  
        // set position at index 1
        charBuffer.position(1);
  
        // print the char buffer
        System.out.println("CharBuffer before flip: "
                           + Arrays.toString(
                                 charBuffer.array())
                           + "\nPosition: "
                           + charBuffer.position()
                           + "\nLimit: "
                           + charBuffer.limit());
  
        // Flip the charBuffer
        // using flip() method
        charBuffer.flip();
  
        // print the byte buffer
        System.out.println("\nCharBuffer after flip: "
                           + Arrays.toString(
                                 charBuffer.array())
                           + "\nPosition: "
                           + charBuffer.position()
                           + "\nLimit: "
                           + charBuffer.limit());
    }
}
输出:
CharBuffer before flip: [a, b, c]
Position: 1
Limit: 3

CharBuffer after flip: [a, b, c]
Position: 0
Limit: 1

示例 2:

// Java program to demonstrate
// flip() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // defining and allocating CharBuffer
        // using allocate() method
        CharBuffer charBuffer
            = CharBuffer.allocate(4);
  
        // append char value in charBuffer
        // using append() method
        charBuffer.append('a');
        charBuffer.append('b');
  
        // print the char buffer
        System.out.println("CharBuffer before flip: "
                           + Arrays.toString(
                                 charBuffer.array())
                           + "\nPosition: "
                           + charBuffer.position()
                           + "\nLimit: "
                           + charBuffer.limit());
  
        // Flip the charBuffer
        // using flip() method
        charBuffer.flip();
  
        // print the char buffer
        System.out.println("CharBuffer before flip: "
                           + Arrays.toString(
                                 charBuffer.array())
                           + "\nPosition: "
                           + charBuffer.position()
                           + "\nLimit: "
                           + charBuffer.limit());
    }
}
输出:
CharBuffer before flip: [a, b, c,  ]
Position: 3
Limit: 4
CharBuffer before flip: [a, b, c,  ]
Position: 0
Limit: 3

参考: https://docs.oracle.com/javase/9/docs/api/ Java/nio/CharBuffer.html#flip–