Skip to content
Related Articles

Related Articles

Improve Article

CharBuffer subSequence() methods in Java with Examples

  • Last Updated : 30 Sep, 2019
Geek Week

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:

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:

Examples 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);
        }
    }
}
Output:
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




// 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:
index is out of bound
Exception throws: java.lang.IndexOutOfBoundsException

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/CharBuffer.html#subSequence-int-int-

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :