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

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

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

Java.nio.CharBuffer 类subSequence()方法用于创建一个新的字符缓冲区,该缓冲区表示该缓冲区相对于当前位置的指定子序列。

新缓冲区将共享此缓冲区的内容;也就是说,如果这个缓冲区的内容是可变的,那么对一个缓冲区的修改将导致另一个缓冲区被修改。新缓冲区的容量就是这个缓冲区的容量,它的位置是 position() + start,它的限制是 position() + end。当且仅当此缓冲区是直接的时,新缓冲区将是直接的,并且当且仅当此缓冲区是只读的时,它将是只读的。

句法:

public abstract CharBuffer
    subSequence(int start, int end)

参数:此方法采用以下参数。

  • start –子序列中第一个字符相对于当前位置的索引;必须是非负数且不大于剩余()。
  • end –子序列中最后一个字符之后的字符的索引,相对于当前位置;必须不小于 start 且不大于剩余()。

返回值:此方法返回新的字符缓冲区。

异常:如果 start 和 end 的前提条件不成立,此方法将抛出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-