Java CharBuffer subSequence()方法及实例

Java CharBuffer subSequence()方法及实例

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

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

语法

public abstract CharBuffer
    subSequence(int start, int end)

参数: 该方法需要以下参数。

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

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

异常: 如果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-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 参考指南