Stack subList() method in Java with Example
Last Updated :
24 Dec, 2018
Improve
The subList() method of Java.util.Stack class is used to return a view of the portion of this Stack between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned Stack is empty.)
The returned Stack is backed by this Stack, so non-structural changes in the returned Stack are reflected in this Stack, and vice-versa. The returned Stack supports all of the optional Stack operations.
Syntax:
Java
Java
Java
public Stack subList(int fromIndex, int toIndex)Parameters: This method takes the following argument as a parameter.
- fromIndex - low endpoint (inclusive) of the subList
- toIndex - high endpoint (exclusive) of the subList
- IndexOutOfBoundsException - if an endpoint index value is out of range (fromIndex size)
- IllegalArgumentException - if the endpoint indices are out of order (fromIndex > toIndex)
// Java program to demonstrate
// subList() method
// for String value
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Creating object of Stack<Integer>
Stack<String>
stack = new Stack<String>();
// Populating stack1
stack.add("A");
stack.add("B");
stack.add("C");
stack.add("D");
stack.add("E");
// print stack
System.out.println("Original stack: "
+ stack);
// getting the subList
// using subList() method
List<String> stack2 = stack.subList(2, 4);
// print the subList
System.out.println("SubStack of stack: "
+ stack2);
}
catch (Exception e) {
System.out.println(e);
}
}
}
// Java program to demonstrate
// subList() method
// for String value
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Creating object of Stack<Integer>
Stack<String>
stack = new Stack<String>();
// Populating stack1
stack.add("A");
stack.add("B");
stack.add("C");
stack.add("D");
stack.add("E");
// print stack
System.out.println("Original stack: "
+ stack);
// getting the subList
// using subList() method
List<String> stack2 = stack.subList(2, 4);
// print the subList
System.out.println("SubStack of stack: "
+ stack2);
}
catch (Exception e) {
System.out.println(e);
}
}
}
Output:
Example 2: For IndexOutOfBoundsException
Original stack: [A, B, C, D, E] SubStack of stack: [C, D]
// Java program to demonstrate
// subList() method
// for IndexOutOfBoundsException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Creating object of Stack<Integer>
Stack<String>
stack = new Stack<String>();
// Populating stack1
stack.add("A");
stack.add("B");
stack.add("C");
stack.add("D");
stack.add("E");
// print stack
System.out.println("Original stack: "
+ stack);
// getting the subList
// using subList() method
System.out.println("\nEnd index value is out of range");
List<String> stack2 = stack.subList(2, 7);
// print the subList
System.out.println("SubStack of stack: "
+ stack2);
}
catch (Exception e) {
System.out.println(e);
}
}
}
// Java program to demonstrate
// subList() method
// for IndexOutOfBoundsException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Creating object of Stack<Integer>
Stack<String>
stack = new Stack<String>();
// Populating stack1
stack.add("A");
stack.add("B");
stack.add("C");
stack.add("D");
stack.add("E");
// print stack
System.out.println("Original stack: "
+ stack);
// getting the subList
// using subList() method
System.out.println("\nEnd index value is out of range");
List<String> stack2 = stack.subList(2, 7);
// print the subList
System.out.println("SubStack of stack: "
+ stack2);
}
catch (Exception e) {
System.out.println(e);
}
}
}
Output:
Example 3: For IllegalArgumentException
Original stack: [A, B, C, D, E] End index value is out of range java.lang.IndexOutOfBoundsException: toIndex = 7
// Java program to demonstrate
// subList() method
// for IllegalArgumentException
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// Creating object of Stack<Integer>
Stack<String>
stack = new Stack<String>();
// Populating stack1
stack.add("A");
stack.add("B");
stack.add("C");
stack.add("D");
stack.add("E");
// print stack
System.out.println("Original stack: "
+ stack);
// getting the subList
// using subList() method
System.out.println("\nEndpoint indices "
+ "are out of order"
+ " (fromIndex > toIndex)");
List<String> stack2 = stack.subList(7, 2);
// print the subList
System.out.println("SubStack of stack: "
+ stack2);
}
catch (Exception e) {
System.out.println(e);
}
}
}
// Java program to demonstrate
// subList() method
// for IllegalArgumentException
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// Creating object of Stack<Integer>
Stack<String>
stack = new Stack<String>();
// Populating stack1
stack.add("A");
stack.add("B");
stack.add("C");
stack.add("D");
stack.add("E");
// print stack
System.out.println("Original stack: "
+ stack);
// getting the subList
// using subList() method
System.out.println("\nEndpoint indices "
+ "are out of order"
+ " (fromIndex > toIndex)");
List<String> stack2 = stack.subList(7, 2);
// print the subList
System.out.println("SubStack of stack: "
+ stack2);
}
catch (Exception e) {
System.out.println(e);
}
}
}
Output:
Original stack: [A, B, C, D, E] Endpoint indices are out of order (fromIndex > toIndex) java.lang.IllegalArgumentException: fromIndex(7) > toIndex(2)