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


Java CharBuffer flip()用法及代碼示例


java.nio.CharBuffer類的flip()方法用於翻轉此緩衝區。將限製設置為當前位置,然後將該位置設置為零。如果定義了標記,則將其丟棄。在序列完成channel-read或放置操作之後,調用此方法為序列進行準備的channel-write或相對get操作。

例如:

buf.put(magic); // Prepend header
in.read(buf); // Read data into rest of buffer
buf.flip(); // Flip buffer
out.write(buf); // Write header + data to channel



當將數據從一個地方傳輸到另一個地方時,通常將此方法與compact()方法結合使用。

用法:

public final CharBuffer flip()

返回值:此方法返回此緩衝區。

下麵是說明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-



相關用法


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