CharBuffer subSequence() methods in Java with Examples
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.
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().
Return Value: This method returns the new character buffer.
Exception: This method throws IndexOutOfBoundsException if the preconditions on start and end do not hold.
Below are the examples to illustrate the subSequence() method:
Original CharBuffer : [a, b, c, d, e] Position: 0 Limit: 5 New Charbuffer: [a, b, c, d, e] Position: 2 Limit: 4
Examples 2: For IndexOutOfBoundsException
index is out of bound Exception throws: java.lang.IndexOutOfBoundsException
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.