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


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


java.nio.CharBuffer類的subSequence()方法用於創建一個新的字符緩衝區,該字符緩衝區表示此緩衝區相對於當前位置的指定子序列。

新緩衝區將共享該緩衝區的內容;也就是說,如果該緩衝區的內容是可變的,則對一個緩衝區的修改將導致另一個緩衝區被修改。新緩衝區的容量將是此緩衝區的容量,其位置將是position() +起始,其限製將是position() +結束。當且僅當該緩衝區是直接緩衝區時,新緩衝區才是直接緩衝區;當且僅當該緩衝區是隻讀緩衝區時,新緩衝區才是隻讀緩衝區。

用法:


public abstract CharBuffer
    subSequence(int start, int end)

參數:此方法采用以下參數。

  • start -子序列中第一個字符相對於當前位置的索引;必須為非負數,且不得大於remaining()。
  • end -子序列中最後一個字符之後的字符相對於當前位置的索引;必須不小於開始且不大於remaining()。

返回值:此方法返回新的字符緩衝區。

異常:如果開始和結束的前提條件不成立,則此方法將引發IndexOutOfBoundsException。

下麵是說明subSequence()方法的示例:

範例1:

// Java program to demonstrate 
// subSequence() method 
  
import java.nio.*; 
import java.util.*; 
import java.io.IOException; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        try { 
  
            // Declare and initialize the char array 
            char[] cb = { 'a', 'b', 'c', 'd', 'e' }; 
  
            // wrap the char array into CharBuffer 
            // using wrap() method 
            CharBuffer charBuffer 
                = CharBuffer.wrap(cb); 
  
            // charBuffer.position(3); 
  
            // Getting new CharBuffer 
            // using subSequence() method 
            CharBuffer cb2 = charBuffer.subSequence(2, 4); 
  
            // print the byte buffer 
            System.out.println("Original CharBuffer:"
                               + Arrays.toString( 
                                     charBuffer.array()) 
                               + "\nPosition:"
                               + charBuffer.position() 
                               + "\nLimit:"
                               + charBuffer.limit() 
                               + "\n\nNew Charbuffer:"
                               + Arrays.toString( 
                                     cb2.array()) 
                               + "\nPosition:"
                               + cb2.position() 
                               + "\nLimit:"
                               + cb2.limit()); 
        } 
        catch (IndexOutOfBoundsException e) { 
            System.out.println("index is out of bound"); 
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
Original CharBuffer:[a, b, c, d, e]
Position:0
Limit:5

New Charbuffer:[a, b, c, d, e]
Position:2
Limit:4

範例2:對於IndexOutOfBoundsException

// Java program to demonstrate 
// subSequence() method 
  
import java.nio.*; 
import java.util.*; 
import java.io.IOException; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
        try { 
  
            // Declare and initialize the char array 
            char[] cb = { 'a', 'b', 'c', 'd', 'e' }; 
  
            // wrap the char array into CharBuffer 
            // using wrap() method 
            CharBuffer charBuffer 
                = CharBuffer.wrap(cb); 
  
            // charBuffer.position(3); 
  
            // Getting new CharBuffer 
            // using subSequence() method 
            CharBuffer cb2 
                = charBuffer.subSequence(-2, 4); 
  
            // print the byte buffer 
            System.out.println("Original CharBuffer:"
                               + Arrays.toString( 
                                     charBuffer.array()) 
                               + "\nPosition:"
                               + charBuffer.position() 
                               + "\nLimit:"
                               + charBuffer.limit() 
                               + "\n\nNew Charbuffer:"
                               + Arrays.toString( 
                                     cb2.array()) 
                               + "\nPosition:"
                               + cb2.position() 
                               + "\nLimit:"
                               + cb2.limit()); 
        } 
        catch (IndexOutOfBoundsException e) { 
            System.out.println("index is out of bound"); 
            System.out.println("Exception throws:" + e); 
        } 
    } 
}
輸出:
index is out of bound
Exception throws:java.lang.IndexOutOfBoundsException

參考: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#subSequence-int-int-



相關用法


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