当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。