The subSequence() method of java.nio.CharBuffer Class is used to create a new character buffer that represents the specified subsequence of this buffer, relative to the current position.
The new buffer will share this buffer's content; that is, if the content of this buffer is mutable then modifications to one buffer will cause the other to be modified. The new buffer's capacity will be that of this buffer, its position will be position() + start, and its limit will be position() + end. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.
Syntax:
Java
Java
public abstract CharBuffer
subSequence(int start, int end)
Parameter: This method takes the following arguments.
- start - The index, relative to the current position, of the first character in the subsequence; must be non-negative and no larger than remaining().
- end - The index, relative to the current position, of the character following the last character in the subsequence; must be no smaller than start and no larger than remaining().
// 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);
}
}
}
Output:
Examples 2: For IndexOutOfBoundsException
Original CharBuffer : [a, b, c, d, e] Position: 0 Limit: 5 New Charbuffer: [a, b, c, d, e] Position: 2 Limit: 4
// 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);
}
}
}
Output:
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#subSequence-int-int-index is out of bound Exception throws: java.lang.IndexOutOfBoundsException